7

I am trying to work out how to get the feature information onclick for more than one layer, apart from variable etc my code is very simmilar to the openlayers example (http://openlayers.org/en/latest/examples/getfeatureinfo-tile.html)

map.on('singleclick', function(evt) {
    document.getElementById('info').innerHTML = '';
    var viewResolution = /** @type {number} */ (view.getResolution());
    var url = wmsSource.getGetFeatureInfoUrl(
        evt.coordinate, viewResolution, 'EPSG:3857',
        {'INFO_FORMAT': 'text/html'});
    if (url) {
      document.getElementById('info').innerHTML =
          '<iframe seamless src="' + url + '"></iframe>';
    }
});

For example instead of just querying the wmsSource layer/variable I would like to query more than one?

6
  • The example you reference is openlayers 3, but the question and tags mention openlayers 2. An example of a GetFeatureInfo request for OL 2 can be found here: dev.openlayers.org/examples/getfeatureinfo-control.html. Which version are you interested in?
    – nmtoken
    Aug 31, 2016 at 6:56
  • would prefer to use ol3, is there any benefit to either version?
    – cmca
    Aug 31, 2016 at 7:03
  • If you want to use OL3 then you should edit your question to mention OL 3 and not OL2. You say you prefer to use OL3, but then ask about benefits of using one version over another, so what is the basis of this preference? if you're after a solution using either version of OL then you should edit the question to mention both versions and discuss the reasons for your preference.
    – nmtoken
    Aug 31, 2016 at 7:12
  • the question and tags were edited by a moderator to state ol2 i believe not sure why but that might be where the confusion is coming from?
    – cmca
    Aug 31, 2016 at 7:17
  • Thanks for doing the clarification updates. Looking at the edit history for the question I think the original tag you gave was openlayers-2, and title just openlayers. gis.stackexchange.com/posts/208979/revisions
    – nmtoken
    Aug 31, 2016 at 7:24

2 Answers 2

2

You should execute a GetFeatureInfo for each layer that you wish to query. Assuming you have the layer names (LAYERS) stored in a string, below code should point you in the right direction:

var layerNamesArray = layers.split(";");
map.on('singleclick', function(evt) {
    layerNamesArray.forEach(function (layerName) {
        var viewResolution = (view.getResolution());

        //get a fetaure info query for each layer
        var url = wmsSource.getGetFeatureInfoUrl(
            evt.coordinate, viewResolution, 'EPSG:3857',
            {'INFO_FORMAT': 'text/html', 'QUERY_LAYERS' : layerName});

        //assuming you use jquery
        $.get(url, function (data) {
            //append the returned html data
            $("#info").append($("div").html(data));
        });
    });
});

The key is the 'QUERY_LAYERS' parameter here.

1
  • Why the loop is required here? The key QUERY_LAYERS can accept values in comma added value. Eg layer1,layer2,layer3,...,...
    – Basil
    Dec 2, 2021 at 8:48
0

You have to query it on each layer. So you should keep track of your WMS layer sources and call getGetFeatureInfoUrl on each of them, something like that:

wmsSources.forEach(function(source){
    var url = source.getGetFeatureInfoUrl(...);
    ...
});

Additionally you could filter visible layers using layer.getVisible() method.

You could also retrieve all layer sources from map object like that:

map.getLayers().forEach(...);

But in this case you have to filter by wms layers, for example by checking with something like:

if (source instanceof ol.source.TileWMS) { ... }
1
  • A single GetFeatureInfo request though does allow more than one layer to be queried.
    – nmtoken
    Dec 2, 2018 at 10:59

Your Answer

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

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