Reputation: 3083
How does one get the layer for a given feature? When a user selects a feature on the map I need to access some layer properties. In ol2 I used feature.layer.
Upvotes: 1
Views: 1081
Reputation: 14168
For now, it's not possible to walk from the feature to the layer but you can create a workaround like:
ol.Feature.prototype.getLayer = function() {
var this_ = this, layer_;
var sameFeature = function(feature){
return (this_ === feature) ? true : false;
};
map.getLayers().forEach(function(layer){
var source = layer.getSource();
if(source instanceof ol.source.Vector){
var features = source.getFeatures();
if(features.length > 0){
var found = features.some(sameFeature);
if(found){
layer_ = layer;
}
}
}
});
return layer_;
};
And then use like:
var layer = feature.getLayer();
Upvotes: 3