Steve Kamau
Steve Kamau

Reputation: 2785

How to add/remove markers with MAPBOX android SDK

I am adding markers using the recommend way by adding the marker to the map style as a new layer

    List<Feature> symbolLayerIconFeatureList .....

    @Override
    public void onMapReady(@NonNull final MapboxMap mapboxMap) {

        symbolLayerIconFeatureList = new ArrayList<>();
        symbolLayerIconFeatureList.add(Feature.fromGeometry(
                Point.fromLngLat(-57.225365, -33.213144)));
        symbolLayerIconFeatureList.add(Feature.fromGeometry(
                Point.fromLngLat(-54.14164, -33.981818)));
        symbolLayerIconFeatureList.add(Feature.fromGeometry(
                Point.fromLngLat(-56.990533, -30.583266)));

        mapboxMap.setStyle(new Style.Builder().fromUri("mapbox://styles/mapbox/cjf4m44iw0uza2spb3q0a7s41")

        // Add the SymbolLayer icon image to the map style
                .withImage(ICON_ID, BitmapFactory.decodeResource(
                        MainActivity.this.getResources(), R.drawable.red_marker))

        // Adding a GeoJson source for the SymbolLayer icons.
                .withSource(new GeoJsonSource(SOURCE_ID,
                        FeatureCollection.fromFeatures(symbolLayerIconFeatureList)))

        // Adding the actual SymbolLayer to the map style. An offset is added that the bottom of the red
        // marker icon gets fixed to the coordinate, rather than the middle of the icon being fixed to
        // the coordinate point. This is offset is not always needed and is dependent on the image
        // that you use for the SymbolLayer icon.
                .withLayer(new SymbolLayer(LAYER_ID, SOURCE_ID)
                        .withProperties(PropertyFactory.iconImage(ICON_ID),
                                iconAllowOverlap(true),
                                iconOffset(new Float[]{0f, -9f}))
                ), new Style.OnStyleLoaded() {
            @Override
            public void onStyleLoaded(@NonNull Style style) {

        // Map is set up and the style has loaded. Now you can add additional data or make other map adjustments.


            }
        });
    }

I would like to add or remove one of the markers at will during runtime, let's assume this comes after an api call. I have tried this:

 markerCoordinates.remove(Feature.fromGeometry(
                        Point.fromLngLat(-57.225365, -33.213144)));
 mapView.invalidate();

But it does not work. Other options suggested i remove the layer but it hasn't worked. Any suggestions?

Upvotes: 1

Views: 2772

Answers (2)

iboalali
iboalali

Reputation: 379

It can be done with a few steps:

  1. Get the layer source with GeoJsonSource source = mapboxMap.getSourceAs(SOURCE_ID);
  2. Update your list of features symbolLayerIconFeatureList by adding or removing any feature you want.
  3. Give your source the new feature list source.setGeoJson(FeatureCollection.fromFeatures(symbolLayerIconFeatureList));

And you are good to go.

Upvotes: 3

Govind Prajapati
Govind Prajapati

Reputation: 206

you can remove marker by sourceId. for ex :- mapStyle.removeSource(sourceId)

Upvotes: 0

Related Questions