Remove Leaflet markers _leaflet_id

I recommend putting all markers into a layerGroup or a featureGroup, such as:

var markerGroup = L.layerGroup().addTo(map);

Then you add the markers to the group:

L.marker([52.520861, 13.409564]).addTo(markerGroup);

Then you can easily remove the markers from the map by using the removeLayer method on that group.

For example, if you want to remove the marker with the ID 219, then you would have to do the following:

markerGroup.removeLayer(219)

…BUT the _leaflet_id count goes up…

This is a feature, not a bug.

_leaflet_id is a private property of the layers, which should never be handled by users of the Leaflet library unless strictly necessary. You shouldn't care about this value, and you should keep references to your layers somewhere else.

…I want to reset the variable [to] zero

You shouldn't. The greatest _leaflet_id used is a static property of the L.Util namespace (L.Util.lastId), and behaves like a singleton (i.e. if more than one map is on the same webpage, both maps share the autoincrementing counter). If you overwrite its value, it might result in broken functionality.

Tags:

Leaflet

Delete