I have two *.geojson-files in my assets-folder and I am loading these points to the map. But I am trying to change the default mapbox-markers to custom colored ones (e.g. green and black markers).
I created a marker as a vector asset, but when I use that instead of "R.drawable.map_marker_light" I get the following error: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap$Config android.graphics.Bitmap.getConfig()' on a null object reference
I couldn't find a current solution for my problem.
public void GeoJSONToMap(final String sourceId, final String layerId, final String asset_id) {
mapboxMap.getStyle(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
try {
GeoJsonSource source = new GeoJsonSource(sourceId, new URI(asset_id));
style.addSource(source);
Bitmap icon;
if (layerId.equals("first-layer-id")) {
icon = BitmapFactory.decodeResource(getResources(), R.drawable.mapbox_marker_icon_default);
} else {
icon = BitmapFactory.decodeResource(getResources(), R.drawable.map_marker_light);
}
style.addImage(layerId + " marker", icon);
SymbolLayer symbolLayer = new SymbolLayer(layerId, sourceId);
symbolLayer.setProperties(
iconImage(layerId + " marker"),
iconAllowOverlap(true),
iconIgnorePlacement(true)
);
style.addLayer(symbolLayer);
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
});
}
Ok i found the solution through github. I added the following code to my MainActivity and adjusted the if-else part. Then I got my custom markers.
public Bitmap getBitmap() {
Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.marker_book, null);
Bitmap mBitmap = BitmapUtils.getBitmapFromDrawable(drawable);
if (mBitmap != null && mBitmap.getConfig() != Bitmap.Config.ARGB_8888) {
mBitmap = mBitmap.copy(Bitmap.Config.ARGB_8888, false);
}
return mBitmap;
}