How to add/remove markers with MAPBOX android SDK

删除回忆录丶 提交于 2019-12-11 17:28:32

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/57991180/how-to-add-remove-markers-with-mapbox-android-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!