简体   繁体   English

在打开的图层中获取具有特定功能的图层3

[英]Get the layer given a feature in open layers3

How do you get a feature's layer in open layers 3? 如何在开放的第3层中获得要素的图层?

Past versions of open layers had a layer property on each feature . 以前版本的开放层在每个要素上都具有 layer属性。 This made it easy to apply layer specific styling to features or to organize features by layer. 这样可以轻松地将特定于图层的样式应用于要素或按图层组织要素。

Open layers 3 is missing this property. 开放层3缺少此属性。 I'm using ol.map.forEachFeatureAtPixel to get features on hover. 我正在使用ol.map.forEachFeatureAtPixel获取悬停功能。

    // Loop through all features a given pixel
    var feature = map.forEachFeatureAtPixel(pixel, function(feature, layer) {
        console.log(layer);
        return feature;      // just return the first feature
    });

Just wanted to answer my own question in case others are having this issue. 只是想回答我自己的问题,以防其他人遇到这个问题。 OL3 forum disscussion on this. OL3论坛对此进行了讨论

The solution here to to follow the OL3 hover example and pass in a layer to the ol.map.forEachFeatureAtPixel function. 遵循OL3悬停示例并将此处传递给ol.map.forEachFeatureAtPixel函数的层的解决方案。 This paramater is not in the documentation so it's hard to find, but it will get you the layer. 该参数不在文档中,因此很难找到,但是它将为您提供帮助。 I'm not sure how this interacts with features on multiple layers. 我不确定它如何与多层要素交互。

   // Loop through all features at this pixel but just return the top level feature
    var fl = map.forEachFeatureAtPixel(pixel, function(feature, layer) {
        return {'feature':feature, 'layer':layer};
    });

    var feature = fl.feature,   feature
        layer = fl.layer;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM