how to clear multiple polylines

3,940 views
Skip to first unread message

Vince Lowe

unread,
Jan 29, 2013, 9:47:58 AM1/29/13
to leafl...@googlegroups.com
Hi,

I am struggling to clear multiple polylines, i can only clear the newest.

See below, imagine addPolyline is called multiple times, then clearPolylines is called once.

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

Vince Lowe

unread,
Jan 29, 2013, 3:38:42 PM1/29/13
to leafl...@googlegroups.com
sorted using layer groups ;)

Ladislav Siegl

unread,
May 17, 2013, 11:51:28 AM5/17/13
to leafl...@googlegroups.com
Hi, I have the same problem,can U help me?

Jake Wilson

unread,
May 17, 2013, 12:30:15 PM5/17/13
to leafl...@googlegroups.com
The easiest solution is to group polylines together into LayerGroups:

var polylineArray = [
  L.polyline([[4,5],[6,8]], { color: 'red' }),
  L.polyline([[4,5],[6,8]], { color: 'red' }),
  L.polyline([[4,5],[6,8]], { color: 'red' }),
  L.polyline([[4,5],[6,8]], { color: 'red' })
];

var polylines = L.layerGroup(polylineArray);

// Add all polylines to the map
polylines.addTo(map);

// Remove polylines from map
map.removeLayer(polylines);

Reply all
Reply to author
Forward
0 new messages