0

I want to put a drawable programatically to my MarkerOptions in Google maps. I get the icon like String and later I try to use it to put the icon of the MarkerOptions.

I am using this code:

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

But I can´t do like this because there is an issue in : mis_markers.icon(BitmapDescriptorFactory.fromResource(drawable));

Can somebody help me? Thanks a lot

Marian Paździoch
  • 8,813
  • 10
  • 58
  • 103
Elena
  • 181
  • 2
  • 12

3 Answers3

1
//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));
koutuk
  • 832
  • 1
  • 8
  • 17
0

Follow below doc :-

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

see below code to add marker on your map.

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

see this

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

Community
  • 1
  • 1
duggu
  • 37,851
  • 12
  • 116
  • 113
0

You are mixing up BitmapDescriptorFactory.fromResource() and BitmapDescriptorFactory.fromBitmap(). This is the correct use of fromResource(). It takes the resource id as the argument:

String icono = mis_localizaciones.get(i).getIcon();
int id = getResources().getIdentifier(icono, "drawable", getPackageName());           
mi_marker.icon(BitmapDescriptorFactory.fromResource(id));
FD_
  • 12,947
  • 4
  • 35
  • 62