我想在谷歌地图中以编程方式将可绘制对象放入我的标记选项中。我得到了像字符串这样的图标,后来我尝试使用它来放置标记选项的图标。
我正在使用以下代码:
String icono = mis_localizaciones.get(i).getIcon();
int id = getResources().getIdentifier(icono, "drawable", getPackageName());
Drawable drawable = getResources().getDrawable(id);
mi_marker.icon(BitmapDescriptorFactory.fromResource(drawable));
但我不能这样做,因为存在以下问题:mis_markers.icon(BitmapDescriptorFactory.fromResource(drawable));
有人可以帮助我吗?多谢
//make Sure icono String doesnt contain extension of file
// eg: flower.png is not a valid input for icono
// eg flower is valid input
String icono = mis_localizaciones.get(i).getIcon();
int id = getResources().getIdentifier(icono, "drawable",getPackageName());
// In id variable you aleady got drawable image refrence
mi_marker.icon(BitmapDescriptorFactory.fromResource(id));
按照以下文档:-
https://developers.google.com/maps/documentation/android/marker
请参阅下面的代码以在地图上添加标记。
Marker melbourne = mMap.addMarker(new MarkerOptions()
.position(MELBOURNE)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
看到这里
如何在谷歌地图安卓API v2中显示多个带有不同图标的标记?
你混淆了BitmapDescriptorFactory.fromResource()
和BitmapDescriptorFactory.fromBitmap()
.这是fromResource()
的正确用法。它将资源 ID 作为参数:
String icono = mis_localizaciones.get(i).getIcon();
int id = getResources().getIdentifier(icono, "drawable", getPackageName());
mi_marker.icon(BitmapDescriptorFactory.fromResource(id));