4

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.

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!

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 ? Any help ?

Jonatas Walker
  • 13,583
  • 5
  • 53
  • 82
Sheyar
  • 51
  • 2
  • 7
  • At least for me, it's not clear what you want. Maybe you want a layer filter? See http://gis.stackexchange.com/a/127072/50718. – Jonatas Walker Feb 05 '16 at 13:28
  • I saw this and tried it .. but it gives me properties just for the layer which i have put in the filter and if i click on another layer it doesn't give any properties. In the same pixel which i click i have three features under each other what i want when i click on a feature from the highest layer to give me properties for this feature just in this layer and not all preporties for the features in the pixel, is it clear now ? – Sheyar Feb 05 '16 at 13:52
  • Not really. Maybe you put this on a fiddle. – Jonatas Walker Feb 05 '16 at 14:04

1 Answers1

2

You can't specify a single click for each layer but according to the api doc of forEachFeatureAtPixel function :

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

oterral
  • 471
  • 3
  • 9