如何使用MAPBOX android SDK添加/删除标记 [英] How to add/remove markers with MAPBOX android SDK

查看:529
本文介绍了如何使用MAPBOX android SDK添加/删除标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过将标记添加到地图样式中作为新图层

    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.


            }
        });
    }

我想在运行时随意添加或删除其中一个标记,让我们假设这是在api调用之后进行的. 我已经尝试过了:

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?

推荐答案

可以通过几个步骤完成:

It can be done with a few steps:

  1. 使用GeoJsonSource source = mapboxMap.getSourceAs(SOURCE_ID);
  2. 获取图层源
  3. 通过添加或删除所需的任何功能来更新功能列表symbolLayerIconFeatureList.
  4. 为您的source新功能列表source.setGeoJson(FeatureCollection.fromFeatures(symbolLayerIconFeatureList));
  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));

你很好.

这篇关于如何使用MAPBOX android SDK添加/删除标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆