How to clear all markers?

22,937 views
Skip to first unread message

Marian Steinbach

unread,
Feb 22, 2012, 7:17:21 AM2/22/12
to leafl...@googlegroups.com
Hi!

I'd like to delete all markers from the map. Seems as if I can't find the according method in the documentation. How would I do that?

Thanks!

Marian

Vladimir Agafonkin

unread,
Feb 22, 2012, 7:21:05 AM2/22/12
to leafl...@googlegroups.com
Hi Marian,

The best way to do this is to use a layer group for markers you add. That way you can do group.clearLayers() to remove all markers from it at once. See the example here: http://leaflet.cloudmade.com/examples/layers-control.html 

2012/2/22 Marian Steinbach <marian.s...@gmail.com>

Marian Steinbach

unread,
Feb 22, 2012, 7:25:23 AM2/22/12
to leafl...@googlegroups.com
Thanks, Vladimir! I will try that. You'll here from me if I fail :)

Ludovic

unread,
Mar 13, 2012, 10:20:04 AM3/13/12
to Leaflet
Hi guys,
I wonder if clearLayers() used on a group of markers really deletes
the markers (ie destroys the markers "objects") or only remove the
markers from some kind of layer list (but the objects are still in
memory)? Do you see what I mean?
Ludo

On Feb 22, 1:25 pm, Marian Steinbach <marian.steinb...@gmail.com>
wrote:

Jason Sanford

unread,
Mar 13, 2012, 10:42:29 AM3/13/12
to leafl...@googlegroups.com
Leaflet uses the `delete` method in JavaScript to remove layers from a L.LayerGroup object (https://github.com/CloudMade/Leaflet/blob/master/src/layer/LayerGroup.js#L33). 

According to http://stackoverflow.com/questions/742623/deleting-objects-in-javascript delete only deletes references to objects.

So, I'd imagine if you're not maintaining any other reference to these objects (e.g. myLayers = [marker1, marker2]) they would in fact be moved out of memory.

But I could be wrong.
Reply all
Reply to author
Forward
0 new messages