[GIS] GetFeatureInfo for more than one layer using OpenLayers 3

getfeatureinfoopenlayerswms

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?

Best Answer

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.