如何从Openlayers 3中的功能获取图层? [英] How to get a layer from a feature in Openlayers 3?

查看:2031
本文介绍了如何从Openlayers 3中的功能获取图层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到从选择事件中的某个要素转到可能是其中一部分而无需遍历所有地图图层的所有要素,或者在每个要素中存储人工图层ID的图层的方法。创建。这是不可能的吗?



ol.js 3.7.0
ol.interaction.Selection - > click - > callback(event){event.selected [0]}



在我的应用程序的另一部分中,我想从该功能转到图层以确定该功能使用的样式,特别是它是否可见。



ol.Feature.getStyle()|| ol.Feature - >(layer?) - > getStyle()

解决方案

你可以试试过滤函数:

  var select = new ol.interaction.Select({
condition:...,
filter:function( feature,layer){
console.info(feature);
console.info(layer.get('name'));
}
});

更新



<我想出了这个原型方法,它完成了这项工作:



http://jsfiddle.net/jonataswalker/r242y7ke/

  / ** 
*这是一种解决方法。
*返回关联的图层。
* @param {ol.Map}地图。
* @return {ol.layer.Vector}图层。
* /
ol.Feature.prototype.getLayer = function(map){
var this_ = this,layer_,layersToLookFor = [];
/ **
*使用
* /
var check = function(layer){
var source'填充数组layersToLookFor只有
*图层= layer.getSource();
if(source instanceof ol.source.Vector){
var features = source.getFeatures();
if(features.length> 0){
layersToLookFor.push({
layer:layer,
features:features
});
}
}
};
//遍历地图图层
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){
//这是我们想要的图层
layer_ = obj.layer;
}
});
返回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'));
}
});


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?

ol.js 3.7.0 ol.interaction.Selection -> click -> callback( event ){ 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()

解决方案

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/

/**
 * 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'));
    }
});

这篇关于如何从Openlayers 3中的功能获取图层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆