在 MapBox 上删除多段线

Delete a polyline on MapBox

我想 deleteMapBoxMapView 上跟踪 polyline,但是没有易于使用的方法(例如:清除、删除、删除....)

import com.mapbox.mapboxsdk.views.MapView;
import com.mapbox.mapboxsdk.annotations.PolylineOptions;


private com.mapbox.mapboxsdk.annotations.PolylineOptions plo;
private MapView mapView;
[..]


    public void drawPath(){
        DebugLog.logd("FragmentMapBox", "drawPath()");        
        if (path != null) {            
            List<IGeoPoint> pointsList = path.getListPoints();
            plo = new com.mapbox.mapboxsdk.annotations.PolylineOptions();

            for(IGeoPoint pt : pointList){
                LatLng temp = new LatLng(pt.getLatitude(),pt.getLongitude());
                plo.add(temp);
            }
            plo.color(getResources().getColor(R.color.colorflashy));
            plo.width(4);
            mapView.addPolyline(plo);            
        }
    }

"plo" 这是一个 PolylineOptions

这里是文档:https://www.mapbox.com/android-sdk/api/3.2.0/

我还没找到....

要删除折线,请使用 polyline.remove();

希望对您有所帮助

我找到了回复:

(com.mapbox.mapboxsdk.注释.PolylineOptions())

 mapView.removeAnnotation(plo.getPolyline());

你说的是mapbox中没有remove之类的方法。 我刚刚查看了文档,我告诉你有一个 remove() 方法。

请查看文档。

Docs

如果我们有一个点列表

    final List<LatLng> mListLatLng = new ArrayList<>();
    {
            mListLatLng.add(new LatLng(18.515700, 73.781901));
            mListLatLng.add(new LatLng(18.515800, 73.781902));
            mListLatLng.add(new LatLng(18.515900, 73.781903));
            mListLatLng.add(new LatLng(18.516000, 73.781904));
            mListLatLng.add(new LatLng(18.516100, 73.781905));
            mListLatLng.add(new LatLng(18.516200, 73.781906));
            mListLatLng.add(new LatLng(18.516300, 73.781907));
            mListLatLng.add(new LatLng(18.516400, 73.781908));
            mListLatLng.add(new LatLng(18.516500, 73.781909));
            mListLatLng.add(new LatLng(18.516600, 73.781910));
            mListLatLng.add(new LatLng(18.516700, 73.781911));
            mListLatLng.add(new LatLng(18.516800, 73.781912));
            mListLatLng.add(new LatLng(18.516900, 73.781913));
            mListLatLng.add(new LatLng(18.517000, 73.781914));
    }

要在 mapbox 中添加折线,我们使用此方法。

PolylineOptions polylineOptions = new PolylineOptions()
                .color(Color.WHITE)
                .add(mListLatLng.toArray(new LatLng[mListLatLng.size()]))
                .width(2);

// this is important as this is where you have to save a polyline object as
        Polyline polyline = mapboxMap.addPolyline(polylineOptions); 

现在,您可以轻松调用删除功能

//To remove created polyline from map               
mMapBox.removePolyline(mPolyLine);

注意:如果您打电话给

Polyline polyline = mapboxMap.addPolyline(polylineOptions);

一次又一次,然后您必须确保添加这些行。

if (polyLine != null) {
   polyLine.remove();
   polyLine = null;
}
polyline = mapboxMap.addPolyline(polylineOptions);

我知道这有点晚了,但几天前我遇到了同样的问题并尝试了所有解决方案,但都没有解决我的问题。我发现了一个非常有效的小解决方法。也许它可以帮助下一个遇到这个问题的人。

您肯定在某处设置了地图样式。我有两种地图样式,只需按一下按钮即可切换。要仅删除注释,您可以使用:

    if let annotations = mapView.annotations {
        mapView.removeAnnotations(annotations)
    }

如果您想删除包括多段线在内的所有内容,请使用以下地图样式技巧:

if mapView.styleURL == MGLStyle.darkStyleURL {
        mapView.styleURL = MGLStyle.darkStyleURL //or any other styleURL
} else {
        mapView.styleURL = URL(string: 
        "mapbox://styles/xxxx[Insert your style URL here]")
}

您可能认为它什么都不做,但您错了。这实际上将加载您已经使用的相同地图样式,因此不会更改任何内容,只会删除地图上的所有内容 - 包括任何形状和多段线。 希望这对您有所帮助。

mapboxMap.getStyle { style ->
        val source = style.getSourceAs<GeoJsonSource>(ROUTE_SOURCE_ID)
        source?.setGeoJson(
            LineString.fromPolyline(
                "",
                PRECISION_6
            )
        )
    }

这对我有用