NonowPoney
NonowPoney

Reputation: 992

Delete a polyline on MapBox

I would like delete a polyline traced on a MapView of MapBox, but there are no methods easily usable (for example : clear, delete, remove....)

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" it's a PolylineOptions

Here the documentation : https://www.mapbox.com/android-sdk/api/3.2.0/

I've not found yet....

Upvotes: 0

Views: 4413

Answers (6)

Nickola Jarjous
Nickola Jarjous

Reputation: 73

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

this Did the trick for me

Upvotes: 1

Jan L
Jan L

Reputation: 253

I know this is a bit late but I had the same issue a few days ago and tried all solutions, which didn't cut it for me. I found a small workaround that works surprisingly well. Maybe it helps the next person having that problem.

You surely have set your map style somewhere. I have two map styles that are switched with the press of a button. To remove only annotations, you can use this:

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

If you want to remove everything including polylines, use the following mapstyle trick:

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

You might think that it does nothing but you're wrong. This will actually load the same map style you already use, thus not changing anything but removing everything that was on the map - including any shapes and polylines. Hope this helped.

Upvotes: 0

Rana Ranvijay Singh
Rana Ranvijay Singh

Reputation: 6155

If we have a list of points

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

To add a polyline in mapbox, we use this method.

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

now, you can easily call remove function

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

NOTE : If you are calling

Polyline polyline = mapboxMap.addPolyline(polylineOptions);

again and again then you have to make sure that you add these lines.

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

Upvotes: 0

Kristo
Kristo

Reputation: 1367

You are stating that there is no method such as remove in the mapbox. I just checked the documentation and i am telling you there is a remove() method.

Please check the docs.enter image description here

Docs

Upvotes: 1

NonowPoney
NonowPoney

Reputation: 992

I've found the response :

(com.mapbox.mapboxsdk.annotations.PolylineOptions())

 mapView.removeAnnotation(plo.getPolyline());

Upvotes: 0

saeed
saeed

Reputation: 1965

To remove the Polyline, use polyline.remove();

Hope this will helps you

Upvotes: 0

Related Questions