Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set and get custom attributes of layers in OpenLayers

In OpenLayers library I create layers like so:

var layer = new ol.layer.Tile({
    source: new ol.source.OSM()
});
...

What I want to do is to add some unique attribute to this layer, or something like:

var layer = new ol.layer.Tile({
    source: new ol.source.OSM(),
    customAttr: 'unique_id'
});
...

so that, later I could do this:

map.getLayers().forEach(function(layer){
    if(layer.customAttr === 'unique_id'){
        doSomething();
    }
});

I do not know how to do this in practice.

like image 309
Jacobian Avatar asked Oct 18 '22 18:10

Jacobian


1 Answers

Change if(layer.customAttr === 'unique_id') to if(layer.get("customAttr") === 'unique_id') and it should work. See this fiddle.

like image 198
itsyahani Avatar answered Oct 23 '22 20:10

itsyahani