获取openlayers 3中给定功能的图层

时间:2014-07-08 17:53:53

标签: javascript openlayers openlayers-3

如何在开放图层3中获得要素的图层?

Past versions个开放图层在每个要素上都有图层属性。这样可以轻松地将图层特定样式应用于要素或按层组织要素。

打开图层3缺少此属性。我正在使用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
    });

1 个答案:

答案 0 :(得分:4)

只是想回答我自己的问题以防其他人遇到这个问题。 OL3 forum disscussion就此而言。

此处的解决方案是遵循OL3悬停example并将图层传递给ol.map.forEachFeatureAtPixel函数。这个参数不在文档中,因此很难找到,但它会让你获得该层。我不确定这与多层上的功能如何相互作用。

   // 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;