我正在从服务器获取三种类型的 ID 的纬度和经度,并在谷歌地图 v2 上显示制造商。我在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 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)));
}
最佳答案
我可能会首先声明一个静态查找表:
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);
然后在设置标记的位置和标题时,查找要使用的图标:
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));
关于android - 如何从可绘制的谷歌地图标记中选择不同的图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27463516/