I must first state that my skills at javascript are about the same as my skills in Spanish. I know barely enough to get myself into trouble and then I go looking for help. anyway I have an application that has a few geoJSON layers in it. I have created a map.forEachFeatureAtPixel function to update a div with some information.

The problem is it looks at all of the layers and I just want it to look at one. this way I can create another function to get some different attributes from another layer. I have looked over the documentation and I see the opt_layerFilter option but I dont know how to apply it (due to my lack of Javascript knowledge). If someone could give me a hint here hopefully I will then be able to read the documentation and stop asking these questions.


var displayFeatureInfo = function(pixel ) {
  var feature = map.forEachFeatureAtPixel(pixel, function(feature, layer) {
    return feature;

  var info = document.getElementById('info');
  if (feature) {
    info.innerHTML =  '<div class="panel panel-info"><div class="panel-heading"><h3 class="panel-title">' + feature.get('Name') +
     '</h3></div><div class="panel-body">' + '<dt>API: </dt><dd class="text-info">' + feature.get('API') + '</dd>'
     + '<dt>Status: </dt><dd class="text-info">' + feature.get('Status') +  '</dd>' +
     '<dt>Legal : </dt><dd class="text-info">' + feature.get('Legal') +  '</dd>' +
     '<dt>Site Name: </dt><dd class="text-info">' + feature.get('Site_Name') +  '</dd>'+'</div>';
  } else {
    info.innerHTML = '&nbsp;';

  if (feature !== highlight) {
    if (highlight) {
    if (feature) {
    highlight = feature;


1 Answer 1


You can use the opt_layerFilter argument to forEachFeatureAtPixel. See the documentation at http://openlayers.org/en/master/apidoc/ol.Map.html#forEachFeatureAtPixel.

For example, if vectorLayer is a reference to the layer in which you want to detect features you will use the following:

var feature = map.forEachFeatureAtPixel(pixel, function(feature, layer) {
  return feature;
}, null, function(layer) {
  return layer === vectorLayer;

With this "filter function" layers that are not vectorLayer will be filtered out.

  • thank you for your help. i am pretty new to this javascript stuff so simple examples like this really help me figure out what is going on! Commented Dec 18, 2014 at 15:29
  • In your snippet you can filter just one layer , my question is how could I filter multiple layers or a specified layers ? ANd Thanks in advance ^^
    – b-eyes
    Commented Dec 19, 2014 at 16:23
  • You can apply any filter in the filter function. You could for example do return layer !== vectorLayer if you want to exclude a specific layer.
    – erilem
    Commented Jan 5, 2015 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.