It would be nice to be able to check for a layer presence or remove it without having to keep an explicit reference to it. To that extent I would like to use ad id for the layer that I can compute myself from my data. Is this feasible with leaflet? If no, do you have any clean workaround?

For example with a LayerGroup, the idea is to have a setId(<String> id) function that would allow leveraging the native removeLayer( <String> id ) and getLayer( <String> id ) methods to work on the LayerGroup content by user defined identifiers. Example below:

marker = L.circleMarker(...);
lg = L.layerGroup(...);

1 Answer 1


If you place all your layers in the same L.featureGroup or L.layerGroup, like this:

group = L.featureGroup().addTo(map);
[etc., etc....]

you can iterate over the layers using the .eachLayer method. This will allow you to create a property of any name you like (say, layerID) and assign a unique value to each layer:

var tempID = 1;
group.eachLayer(function(layer) {
  layer.layerID = tempID;
  layer.bindPopup('Layer ID: ' + layer.layerID);

To access that ID later, you can use .eachLayer again to find the matching feature and perform whatever task you like on it. For instance:

deleteFromGroupByID = function (group, ID) {
    group.eachLayer(function(layer) {
    if (layer.feature.properties.layerID === ID) {

Here's an example fiddle of this at work:


If you are using GeoJSON, it is probably best to store this value in the feature properties instead, as that's what they were designed for. In the above code, you'd just replace layer.layerID with layer.feature.properties.layerID. Example:


  • Thanks, that's a nice workaround. So there is no way to leverage the native removeLayer( <String> id ) and getLayer( <String> id ) function for it?
    – a1an
    Mar 8, 2016 at 10:35
  • The <String> id refers to the variable name to which a layer is assigned. For instance, if you create a marker with var mk1 = L.marker(...), then you can remove it using map.removeLayer(mk1). But I gathered you were trying to avoid that (unless you meant something else by "explicit reference"?). Mar 8, 2016 at 11:31
  • Then I am missing what is the difference between "<String> id" and "ILayer" as parameters since I took for granted that ILayer means a variable holding an object implementing the ILayer interface
    – a1an
    Mar 8, 2016 at 14:56
  • 1
    No, actually, I was mistaken. The variable name refers to an ILayer, so my answer is indeed using that method, not the <String> id. The id actually refers to the internal _leaflet_id of the layer, which you can access using L.stamp. For instance, L.stamp(marker) will return the value of marker._leaflet_id, and map.removeLayer(L.stamp(marker)) will remove the marker. However, manually changing the value of marker._leaflet_id would break things so that you wouldn't be able to use removeLayer (or any leaflet functions at all) on marker. Mar 8, 2016 at 18:38
  • 1
    @nathansnider I think @a1an refers to Layer Group methods removeLayer and getLayer, which can accept the layer's internal id. Map equivalent removeLayer do not.
    – ghybs
    Mar 8, 2016 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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