My map has several layers, and I have a check box list that can turn individual layers on and off (ie. hidden/visible).

I'd like to have querying capability, but with so many layers, I think the easiest way might be to only allow querying on whatever layer is visible, and if there are a few visible, only the top layer is queryable.

The tutorials I've seen are only for querying one layer. How would I go about doing this for multiple layers and for only those that are visible? This is a poor attempt, and only works for one layer:

  var displayFeatureInfo = function(pixel) {
    var features = [];
    map.forEachFeatureAtPixel(pixel, function(feature, layer) {
    var container = document.getElementById('information');
    if (features.length > 0) {
      var info = [];
      for (var i = 0, ii = features.length; i < ii; ++i) {
      container.innerHTML = info.join('- ') || '(unknown)';
    } else {
      container.innerHTML = '&nbsp;';

        map.on('click', function(evt) {
    var pixel = evt.pixel;
  • The documentation says by default all visible layers are considered with forEachFeatureAtPixel and the default opt_layerFilter (openlayers.org/en/latest/apidoc/…). Are you sure all features have the same properties (in your case 'County_Name' & 'Name')? – Lars May 4 '17 at 7:48
  • can't you filter using 'layer' parameter in forEachFeatureAtPixel? – Chase Choi May 19 '17 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.