【问题标题】:How to get a layer from a feature in Openlayers 3?如何从 Openlayers 3 中的功能中获取图层?
【发布时间】:2015-07-08 15:52:51
【问题描述】:

如果不遍历我所有地图图层的所有要素,或者在每个要素中存储人工图层 ID创建。这还不可能吗?

ol.js 3.7.0 ol.interaction.Selection -> 点击 -> 回调(事件){ event.selected[0] }

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

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

【问题讨论】:

  • Jonatas 对选择的回答,要使用过滤器,需要额外注意,我必须将通过“过滤器”函数的所有项目存储在本地数组中,并在选择事件中处理程序(回调),将所选特征与这些数组项之一匹配并清除数组。

标签: javascript maps openlayers-3


【解决方案1】:

你可以试试过滤功能:

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

更新

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

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

【讨论】:

  • 我看到了,但是在我的应用程序的另一部分中,我希望从功能转到图层.. 我会将其添加到问题中。但我会尝试这个建议,如果它有效.. 现在已经足够好了:)
  • 如何存储参考以供以后使用?
  • 至于存储外部引用,这是一种方法,但我对这个问题很感兴趣,想知道是否有办法从框架内的功能到层
  • 我也想使用 this.selection.on( 'select', someCallBack );处理选择事件,而不是过滤器调用,而是处理任何事情
  • 我正在做一些测试,但如果没有一些hackish,这并不容易。
【解决方案2】:

在 OL 5.3.0 中,Select 交互对象具有getLayer() 函数来获取最后选择的特征的关联层。示例:

let selectClick = new Select({});
map.addInteraction(selectClick);

selectClick.on('select', function(e) {
    let featureSelected = e.selected[0];
    let layer = selectClick.getLayer(featureSelected);
    console.log(layer); // here you have the selected layer
});

【讨论】:

    【解决方案3】:

    在 Openlayers 4 - map.forEachFeatureAtPixel 可用于获取每个功能的父层。

    在此处查看代码 sn-p:https://stackoverflow.com/a/50415743/2288488

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多