简体   繁体   English

获取已在OpenLayers 3中单击的图层的要素属性

[英]Get a feature properties for the layer which has been clicked in OpenLayers 3

I am working on OpenLayers 3 with Geoserver, I have four vector layers, I am using singleclick event to get properties for each feature and show them in a popup window. 我正在使用Geoserver处理OpenLayers 3,我有四个矢量图层,我使用singleclick事件获取每个功能的属性并在弹出窗口中显示它们。

Now my problem that when i click on a feature from the highest layer i get all properties from all layers which are lower, I used forEachFeatureAtPixel but i don't know how to specify it for each layer! 现在我的问题是,当我点击最高层的一个特征时,我得到了所有较低层的属性,我使用了forEachFeatureAtPixel但我不知道如何为每一层指定它!

Here is my code: 这是我的代码:

var OpenMeters = function (evt) {
content.innerHTML = "";
var feature = map.forEachFeatureAtPixel(evt.pixel,
    function (feature, layer) {
        if (feature) {
            var coord = map.getCoordinateFromPixel(evt.pixel);
            var objeto = feature.getProperties(),propiedades;
            for (propiedades in objeto)
            {
              content.innerHTML += '<b>' + propiedades + '</b> : <i><b>'+ objeto[propiedades]+'</b></i><br />';
            }
            overlay.setPosition(coord);
        } else {
            overlay.setPosition(undefined);
        }
    });
};

map.on('singleclick', OpenMeters);

var select = new ol.interaction.Select();
map.addInteraction(select);

How can i specify singleclick event for each layer ? 如何为每一层指定singleclick事件? Any help ? 有帮助吗?

You can't specify a single click for each layer but according to the api doc of forEachFeatureAtPixel function : 您不能为每个图层指定单击,但是根据forEachFeatureAtPixel函数api文档

Returns:

Callback result, i.e. the return value of last callback execution, or the first truthy callback return value. 

So if your return a value on the first call of the callback you will get the first feature you hit: 因此,如果您在第一次回调时返回一个值,您将获得您点击的第一个功能:

var feature = map.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
  return feature;    
});
if (feature) {
  var coord = map.getCoordinateFromPixel(evt.pixel);
  var objeto = feature.getProperties(),propiedades;
  for (propiedades in objeto) {
    content.innerHTML += '<b>' + propiedades + '</b> : <i><b>'+ objeto[propiedades]+'</b></i><br />';
    }
    overlay.setPosition(coord);
  } else {
    overlay.setPosition(undefined);
  }
};

SNIPPET NOT TESTED SNIPPET没有测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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