简体   繁体   中英

Get the layer given a feature in open layers3

How do you get a feature's layer in open layers 3?

Past versions of open layers had a layer property on each feature . This made it easy to apply layer specific styling to features or to organize features by layer.

Open layers 3 is missing this property. I'm using ol.map.forEachFeatureAtPixel to get features on hover.

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

The solution here to to follow the OL3 hover example and pass in a layer to the ol.map.forEachFeatureAtPixel function. 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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