Get property properties for a layer that was clicked in OpenLayers 3

I am working on OpenLayers 3 with Geoserver, I have four vector layers, I use the event singleclickto get properties for each function and show them in a pop-up window.

Now my problem: when I click on a function from the highest level, I get all the properties from all the layers that I used below forEachFeatureAtPixel, but I don’t know how to specify it for each layer!

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 an event singleclickfor each layer? Any help?

+4
source share
1 answer

, api doc forEachFeatureAtPixel:

Returns:

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

, , , :

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

+1

Source: https://habr.com/ru/post/1627427/


All Articles