MarkerOptions图标以编程方式

时间:2015-01-21 11:16:00

标签: android google-maps google-maps-markers google-maps-android-api-2

我想在Google地图中以编程方式为我的MarkerOptions提供一个可绘制的。我得到像String这样的图标,后来我尝试使用它来放置MarkerOptions的图标。

我正在使用此代码:

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));

中存在问题

有人能帮助我吗?非常感谢

3 个答案:

答案 0 :(得分:0)

按照以下文档: -

https://developers.google.com/maps/documentation/android/marker

请参阅以下代码,在地图上添加标记。

Marker melbourne = mMap.addMarker(new MarkerOptions()
                          .position(MELBOURNE)
                          .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

看到这个

How to display multiple markers with different icons in Google Maps Android API v2?

答案 1 :(得分:0)

//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));

答案 2 :(得分:0)

您正在混淆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));
相关问题