删除MapBox上的折线

时间:2016-02-19 11:43:35

标签: android mapbox

我希望在delete polyline上跟踪MapView MapBox,但没有方法可以轻松使用(例如:清除,删除,删除.. ..)

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);            
        }
    }

&#34; PLO&#34;它是PolylineOptions

此处有文档:https://www.mapbox.com/android-sdk/api/3.2.0/

我还没找到......

5 个答案:

答案 0 :(得分:1)

您声明地图框中没有删除等方法。 我刚检查了文档,我告诉你有remove()方法。

请查看文档。enter image description here

Docs

答案 1 :(得分:0)

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

希望这会对你有所帮助

答案 2 :(得分:0)

我找到了回复:

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

 mapView.removeAnnotation(plo.getPolyline());

答案 3 :(得分:0)

如果我们有一个积分列表

    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);

答案 4 :(得分:0)

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

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

    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]")
}

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