Mapbox Android SDK: 6.7.0
The requirement in the application we are developing is that we have to add multiple markers in different LatLng positions
This can be achieved by symbol layer in the latest SDK 6.7.0.
To add a marker:
Bitmap compassNeedleSymbolLayerIcon = BitmapFactory.decodeResource(
getResources(), R.drawable.compass_needle);
mapboxMap.addImage(AIRCRAFT_MARKER_ICON_ID, compassNeedleSymbolLayerIcon);
GeoJsonSource geoJsonSource = new GeoJsonSource(GEOJSON_SOURCE_ID, Feature.fromGeometry(
Point.fromLngLat(longitude, latitude)));
mapboxMap.addSource(geoJsonSource);
SymbolLayer Layer = new SymbolLayer(AIRCRAFT_LAYER_ID, GEOJSON_SOURCE_ID)
.withProperties(
PropertyFactory.iconImage(AIRCRAFT_MARKER_ICON_ID),
PropertyFactory.iconRotate((float) headDirection),
PropertyFactory.iconIgnorePlacement(true),
PropertyFactory.iconAllowOverlap(true)
);
mapboxMap.addLayer(layer);
To rotate or changing the position of the marker:
GeoJsonSource source = mapboxMap.getSourceAs(GEOJSON_SOURCE_ID);
if (source != null) {
source.setGeoJson(Feature.
fromGeometry(Point.fromLngLat(longitude, latitude)));
layer.setProperties(
PropertyFactory.iconRotate((float) headDirection)
);
}
This above code may not work sometimes when you add the marker in the onMapReady() callback. Because onMapReady() is called before all styles are loaded. Hence add the marker in addOnDidFinishLoadingStyleListener() callback.
mapView.addOnDidFinishLoadingStyleListener(new MapView.OnDidFinishLoadingStyleListener() {
@Override
public void onDidFinishLoadingStyle() {
//add marker here
}
});