Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear all polylines from leaflet map

I am struggling to clear all polylines from my map, i only clear the newest.

var polylines;

// add map polylines
function addPolyline(polyArray, colour) {
    polylines = L.polyline(polyArray, {color: colour});
    polylines.addTo(map);
}

// clear polylines   
function clearPolylines() {
    map.removeLayer(polylines);
}

where addPolylines is called multiple times and clear Polylines is called once. How can i clear all polylines on the map?

like image 640
Vince Lowe Avatar asked Jan 29 '13 14:01

Vince Lowe


1 Answers

You can add the polyline to a layerGroup and easily add/remove it to/from the map. Something like this:

pLineGroup = L.layerGroup()
var latlngs = [
    [45.51, -122.68],
    [37.77, -122.43],
    [34.04, -118.2]
];
this.pLineGroup.addLayer(L.polyline(latlngs, {color: 'red'}))
pLineGroup.addTo(map)
pLineGroup.removeFrom(map)
like image 194
M.Reza Avatar answered Sep 21 '22 09:09

M.Reza