19

I'm trying to define a bunch of Leaflet polygons like this :

var poly = new L.Polygon([
  [10.1840229, 36.8906981],
  [10.1840393, 36.8906669],
  [10.1840989, 36.8906868],
  [10.1840826, 36.890718],
  [10.1840229, 36.8906981]
], {
  'id': 'someId'
});

Then I'm grouping those polygons in a GroupLayer like so :

var group = new L.LayerGroup([poly1, poly2, ..., polyn]);
group.addTo(map);

Can I find those polygons by Id using group.getLayer() ? Or do I need to define the layers/polygons differently to be able to do this ?

ghybs
  • 47,565
  • 6
  • 74
  • 99
AAJ
  • 197
  • 1
  • 2
  • 10

2 Answers2

30

Leaflet assigns it own unique ID to each layer:

var marker = new L.Marker(...);
console.log(marker._leaflet_id) // 24

var polygon = new L.Polygon(...);
console.log(polygon._leaflet_id) // 25

The getLayer method of L.LayerGroup, L.FeatureGroup and L.GeoJSON take those ID's as a parameter:

var layerGroup = L.LayerGroup([marker, polygon]);

layerGroup.getLayer(24); // returns the marker
layerGroup.getLayer(25); // returns the polygon

You could also easily assign your own ID's:

var marker = new L.Marker(...);
marker.id = 'foo';

var polygon = new L.Polygon(...);
polygon.id = 'bar';

And then fetch them like this:

var layerGroup = L.LayerGroup([marker, polygon]);

layerGroup.eachLayer(function (layer) {
    if (layer.id === 'foo') // it's the marker
    if (layer.id === 'bar') // it's the polygon
});

You can easily throw that into a function and include it in L.LayerGroup:

L.LayerGroup.include({
    customGetLayer: function (id) {
        for (var i in this._layers) {
            if (this._layers[i].id == id) {
               return this._layers[i];
            }
        }
    }
});

var layerGroup = L.LayerGroup([marker, polygon]);

layerGroup.customGetLayer('foo'); // returns the marker
layerGroup.customGetLayer('bar'); // returns the polygon

EDIT: Didn't spot the ID in your example untill the indented edit. You can also assign it as an option like in your example and create a custom get function to retreive the layer:

L.LayerGroup.include({
    customGetLayer: function (id) {
        for (var i in this._layers) {
            if (this._layers[i].options.id == id) {
               return this._layers[i];
            }
        }
    }
});

var layerGroup = L.LayerGroup([marker, polygon]);

layerGroup.customGetLayer('foo'); // returns the marker
layerGroup.customGetLayer('bar'); // returns the polygon

If you ever need to identify the type of a layer you can do so by using instanceof:

The instanceof operator tests whether an object has in its prototype chain the prototype property of a constructor.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof

var layerGroup = L.LayerGroup([marker, polygon]);

layerGroup.eachLayer(function (layer) {
    if (layer instanceof L.Marker) // it's the marker
    if (layer instanceof L.Polygon) // it's the polygon
});

But remember when you find yourself making common selections should ideally have put those features in separate layer/featuregroups.

iH8
  • 27,722
  • 4
  • 67
  • 76
  • I'm gonna stick with what you suggested before the edit. It's work ! Thanks a lot ! – AAJ Dec 16 '15 at 23:13
3

The getLayer() method of Layer Group expects a very specific ID: the one that is automatically assigned by Leaflet when "stamping" a layer (e.g. using myId = L.stamp(myLayer)).

Therefore you would not be able to use pre-defined ID's.

If you can work with ID's defined dynamically (i.e. not known in advance), you could easily record those and use them to retrieve your layers (polygons).

ghybs
  • 47,565
  • 6
  • 74
  • 99
  • I still have to use my own IDs for some reason. So the only way around this is through a loop. `for (i=0; i – AAJ Dec 16 '15 at 23:00
  • What do you mean by "wonder if this is a valid layer"? You would probably be interested in iH8's answer (especially the part after the EDIT). Maybe you could also add a default return to cover the case where the specified ID is not within the group (maybe this is the meaning of your wondering?) – ghybs Dec 16 '15 at 23:09