Elena
Elena

Reputation: 181

MarkerOptions icon programmatically

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

Upvotes: 0

Views: 2934

Answers (3)

FD_
FD_

Reputation: 12919

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

Upvotes: 0

koutuk
koutuk

Reputation: 832

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

Upvotes: 1

duggu
duggu

Reputation: 38439

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?

Upvotes: 0

Related Questions