Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
154
rated 0 times [  160] [ 6]  / answers: 1 / hits: 33621  / 12 Years ago, tue, january 29, 2013, 12:00:00

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?


More From » leaflet

 Answers
38

You'll have to remember them all or cheat a bit and peek into map._layers to find them.



EDIT adding sample code by @Ben:



function clearMap() {
for(i in m._layers) {
if(m._layers[i]._path != undefined) {
try {
m.removeLayer(m._layers[i]);
}
catch(e) {
console.log(problem with + e + m._layers[i]);
}
}
}
}

[#80553] Monday, January 28, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mira

Total Points: 460
Total Questions: 108
Total Answers: 99

Location: American Samoa
Member since Fri, Aug 26, 2022
2 Years ago
mira questions
;