H.Karatsanov
H.Karatsanov

Reputation: 319

Mapbox: Problem with visualizing a symbol

So far I followed this example https://docs.mapbox.com/android/plugins/examples/symbol-listener/ for adding a Symbol at specific location with the SymbolManager. However, the image used for the symbol doesn't appear on the map, although the steps which you see in the code snippet below are the same:

symbolManager = new SymbolManager(mapView, mMapboxMap, style);

symbolManager.setIconAllowOverlap(true);    
symbolManager.setIconIgnorePlacement(true); 

SymbolOptions symbolOptions = new SymbolOptions()
                    .withIconImage(String.valueOf(R.drawable.ic_map_start_flag))
                    .withIconSize(2.0f)
                    .withLatLng(newLatLngs.get(0));

startSymbol = symbolManager.create(symbolOptions); 

The drawable is a vector asset which visualizes without any issues on the activities. I have another piece of code after this one, which runs completely fine on the map. There are also no failures to appear in the Log.

Any ideas why it doesn't appear on the map?

Upvotes: 0

Views: 375

Answers (2)

Koch
Koch

Reputation: 594

Just for more clarification, this indeed helped solve my issue when I couldn't find a way to load the drawable due to this SymbolOtions method expecting a String instead of a Drawable : withIconImage(iconImage:String).

Once you load the bitmap with style.addImage/addImageAsync("custom-image-name", iconBitmap).

You can now display it using withIconImage("custom-image-name") of SymbolOptions

Upvotes: 0

H.Karatsanov
H.Karatsanov

Reputation: 319

I've solved it. The thing is that Mapbox doesn't work directly with the resources inside the drawable folder. For that reason we need to create a new Drawable object from our XML file representing the icon and after that add that drawable to the style, used by the onStyleLoaded(@NonNull Style style) method, like so:

Drawable myIcon = getResources().getDrawable( R.drawable.ic_map_start_flag);
style.addImage(IMAGE_START_FLAG, myIcon);

Upvotes: 2

Related Questions