javascript 如何从 Openlayers 3 的功能中获取图层?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31297721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a layer from a feature in Openlayers 3?
提问by ryansstack
I can't find a way to go from a feature in a selection event to a layer that it may be a part of without traversing all the features of all my map layers, or storing an artificial layer ID within every feature at creation. Is this just not possible yet?
如果不遍历所有地图图层的所有要素,或者在创建时在每个要素中存储人工图层 ID,我无法找到从选择事件中的要素到它可能是其中一部分的图层的方法。这是不可能的吗?
ol.js 3.7.0 ol.interaction.Selection -> click -> callback( event ){ event.selected[0] }
ol.js 3.7.0 ol.interaction.Selection -> 点击 -> 回调(事件){ event.selected[0] }
In another part of my app, I would like to go from the feature to the layer to determine the style being used on the feature, specifically whether or not it's visible.
在我的应用程序的另一部分中,我想从功能到图层来确定功能上使用的样式,特别是它是否可见。
ol.Feature.getStyle() || ol.Feature -> (layer?) -> getStyle()
ol.Feature.getStyle() || ol.Feature -> (layer?) -> getStyle()
采纳答案by Jonatas Walker
You could try with the filter function:
您可以尝试使用过滤功能:
var select = new ol.interaction.Select({
condition: ...,
filter: function(feature, layer){
console.info(feature);
console.info(layer.get('name'));
}
});
UPDATE
更新
I came up with this prototypied method, it does the job:
我想出了这个原型方法,它完成了这项工作:
http://jsfiddle.net/jonataswalker/r242y7ke/
http://jsfiddle.net/jonataswalker/r242y7ke/
/**
* This is a workaround.
* Returns the associated layer.
* @param {ol.Map} map.
* @return {ol.layer.Vector} Layer.
*/
ol.Feature.prototype.getLayer = function(map) {
var this_ = this, layer_, layersToLookFor = [];
/**
* Populates array layersToLookFor with only
* layers that have features
*/
var check = function(layer){
var source = layer.getSource();
if(source instanceof ol.source.Vector){
var features = source.getFeatures();
if(features.length > 0){
layersToLookFor.push({
layer: layer,
features: features
});
}
}
};
//loop through map layers
map.getLayers().forEach(function(layer){
if (layer instanceof ol.layer.Group) {
layer.getLayers().forEach(check);
} else {
check(layer);
}
});
layersToLookFor.forEach(function(obj){
var found = obj.features.some(function(feature){
return this_ === feature;
});
if(found){
//this is the layer we want
layer_ = obj.layer;
}
});
return layer_;
};
select.on('select', function(evt){
var feature = evt.selected[0];
if(feature){
var layer = feature.getLayer(map);
console.info(layer.getStyle());
console.info(layer.get('name'));
}
});
回答by ahMarrone
In OL 5.3.0, the Select interaction object has the getLayer()function to get the associated layer of the last selected feature. Example:
在 OL 5.3.0 中,Select 交互对象具有getLayer()函数,用于获取最后选择的要素的关联图层。例子:
let selectClick = new Select({});
map.addInteraction(selectClick);
selectClick.on('select', function(e) {
let featureSelected = e.selected[0];
let layer = selectClick.getLayer(featureSelected);
console.log(layer); // here you have the selected layer
});
回答by Rob
In Openlayers 4 - map.forEachFeatureAtPixel
can be used to get at the parent layer of each feature.
在 Openlayers 4 中 -map.forEachFeatureAtPixel
可用于获取每个功能的父层。
See code snippet here: https://stackoverflow.com/a/50415743/2288488
请参阅此处的代码片段:https: //stackoverflow.com/a/50415743/2288488