7

So I want to remove some markers that are created on a button click. If I use map.removeLayer() it removes the layer and also I set the array.length = 0, which resets the containing layer. However, if I run the function again, it shows the same amount of created markers and array elements (fine so far), BUT the _leaflet_id count goes up, as if the previous markers haven't been deleted.

For example,

enter image description here

I just figured this issue out because also the markers are overlapping at the same position, so they appear bold and the browsers slows down.

Any ideas how to solve this? Basically, if I clear the elements, I want to reset the variable and the visualization to zero, i.e the initial state of the map before I create any markers.

SOLUTION

I had to use markers.clearLayers() in order to remove all the layers inside the marker featureGroup. Before I only did map.removeLayer(markers), which does not delete the featureGroup content, but only removes the markers layer

  • Do you just want to remove specific markers? Just like this or when clicked? – BritishSteel Jul 13 '16 at 15:16
  • I want to reset the initial map state. The issue is, if I create markers via a button click on the same position after deleting a featureGroup, it looks like they are duplicated, so they become "bold" as if there are two overlapping objects. – Alexejevitsch Jul 13 '16 at 15:36
  • It would help to see the javascript code you're using to store your markers, your event handlers, and so on. I suspect of faulty/naïve logic somewhere in there. – IvanSanchez Jul 13 '16 at 16:02
  • See my comment for the accepted answer, please. The solution was: markers.clearLayers(); and I only did map.removeLayers(markers) before, which just removes the markers layer but not its content inside the featureGroup – Alexejevitsch Jul 13 '16 at 16:12
8

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)
  • Ok, this led me to an issue in my code, where I removed only the featureGroup from the map instead of removing the markers from the featureGroup. Thanks! – Alexejevitsch Jul 13 '16 at 15:55
1

…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.

  • 1
    Well, I don't want to set the _leaflet_id to zero, only the variables I'm using. It works to set them to zero, but I get overlapping markers/rectangles, when I create new ones on the same position. That's what I want to solve. – Alexejevitsch Jul 13 '16 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.