Android 如何从drawable for google地图标记中选择不同的图标?
我用三种类型的ID从服务器上获取纬度和经度,并在google map v2上显示makers。我在drawalbe中有三个图标,分别是a.png、b.png和c.png。我想要的是,如果ID==A1,那么选择a.png作为标记图标;如果ID==B2,那么选择b.png作为标记图标;如果ID==C3,那么选择c.png作为标记图标。我该怎么做?。这是我的密码Android 如何从drawable for google地图标记中选择不同的图标?,android,google-maps,google-maps-markers,drawable,Android,Google Maps,Google Maps Markers,Drawable,我用三种类型的ID从服务器上获取纬度和经度,并在google map v2上显示makers。我在drawalbe中有三个图标,分别是a.png、b.png和c.png。我想要的是,如果ID==A1,那么选择a.png作为标记图标;如果ID==B2,那么选择b.png作为标记图标;如果ID==C3,那么选择c.png作为标记图标。我该怎么做?。这是我的密码 user = json.getJSONArray(TAG_GPS); String La
user = json.getJSONArray(TAG_GPS);
String Latitude, Longitude, ID;
LatLng latLngGps;
int a=user.length();
for(int i=0;i<a;i++){
JSONObject c=user.getJSONObject(i);
Latitude=c.getString(TAG_LAT);
Longitude=c.getString(TAG_LONG);
ID=c.getString(TAG_ID);
latLngGps = new LatLng(Double.parseDouble(Latitude),Double.parseDouble(Longitude));
mGoogleMap .addMarker(new MarkerOptions().position(latLngGps).
title("A").icon(BitmapDescriptorFactory.fromResource(R.drawable.a)));
}
user=json.getJSONArray(TAG\u GPS);
字符串纬度、经度、ID;
LatLng latLngGps;
int a=user.length();
对于(int i=0;i我可能首先声明一个静态查找表:
private static final Map<String, Integer> idTagToIcon = new HashMap<String, Integer>();
idTagToIcon.put("A1", R.drawable.a);
idTagToIcon.put("B2", R.drawable.b);
idTagToIcon.put("C3", R.drawable.c);
感谢mate在上面的代码中做了一点小小的修改,比如使用BitmapDescriptor图标来代替可绘制图标
BitmapDescriptor icon = null; // some default icon here?
Integer iconDrawableID = idTagToIcon.get(ID); // lookup to get assigned drawable
if(iconDrawableID != null) {
icon = BitmapDescriptorFactory.fromResource(iconDrawableID);
}
mGoogleMap.addMarker(new MarkerOptions().position(latLngGps).title("A").icon(icon));