android - 如何从可绘制的谷歌地图标记中选择不同的图标?

标签 android google-maps google-maps-markers drawable

我正在从服务器获取三种类型的 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/

相关文章:

android - 将 PowerMock 添加到我的 gradle 文件中,现在在构建时,我得到 ZipException : duplicate entry: org/junit/ClassRule. 类

android - Android线程控制多个纹理 View 会导致奇怪的闪烁

google-maps - 按米缩放谷歌地图标记图像(固定地理大小)

google-maps - 支持HTTPS请求且免费的静态 map API

android - 为什么在 Android 中使用框架来发出 Http 请求而不是使用它自己的类来发出这个?

android - ORMLite - 如何处理连接结果

android - 在 Android 中的 Google map 中显示位置

javascript - 将 infoWindow 包含到 gmaps markclusterer 中时出现问题

ios - 在 iOS 模拟器上显示 GMSMarkers 时发生 GMSThreadException

javascript - 使用 gomap-plugin 隐藏/查看一组标记