如何获取给定功能的图层

时间:2015-08-12 18:21:59

标签: javascript openlayers-3

如何获得给定功能的图层?当用户在地图上选择要素时,我需要访问某些图层属性。在ol2中我使用了feature.layer。

1 个答案:

答案 0 :(得分:1)

目前,无法从功能转到图层,但您可以创建一个解决方法,如:

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_;
};

然后使用:

var layer = feature.getLayer();