0

I am trying to get attributes from multiple layers on a map click, using OpenLayers 3 and layers served from Geoserver.

I found this snippet but it is using an older version of Open Layers, I haven't found anything that works the same way with Open Layers 3

http://dev.openlayers.org/examples/getfeatureinfo-popup.html

JasonBK
  • 539
  • 3
  • 12
  • 37

2 Answers2

0

The OpenLayers example WMS GetFeatureInfo (Layers) shows exactly this.

<script>
  var wmsSource = new ol.source.TileWMS({
    url: 'https://ahocevar.com/geoserver/wms',
 // Add as many layers as you like here!! 
    params: {'LAYERS': 'ne:ne', 'TILED': true},
    serverType: 'geoserver',
    crossOrigin: 'anonymous'
  });

  var wmsLayer = new ol.layer.Tile({
    source: wmsSource
  });

  var view = new ol.View({
    center: [0, 0],
    zoom: 1
  });

  var map = new ol.Map({
    layers: [wmsLayer],
    target: 'map',
    view: view
  });

  map.on('singleclick', function(evt) {
    document.getElementById('info').innerHTML = '';
    var viewResolution = /** @type {number} */ (view.getResolution());
// Here all the layers in the wmsSource will be added to the URL
    var url = wmsSource.getGetFeatureInfoUrl(
        evt.coordinate, viewResolution, 'EPSG:3857',
        {'INFO_FORMAT': 'text/html'});
    if (url) {
      document.getElementById('info').innerHTML =
          '<iframe seamless src="' + url + '"></iframe>';
    }
  });
Ian Turton
  • 10,018
  • 1
  • 28
  • 47
  • I did come across this example, but I wasn't sure how to adapt this to use with a visible map, so that I retrieve all and only the attributes from the layers at that click point. – JasonBK Dec 04 '17 at 17:16
  • soemthing like https://openlayers.org/en/latest/examples/getfeatureinfo-tile.html maybe? – Ian Turton Dec 04 '17 at 17:27
  • I've seen that example too, that gives info for one layer. I need something like the example i posted in my question, which gives formatted info for two layers on a single click – JasonBK Dec 04 '17 at 18:22
  • Then use the first example in the second one to query all layers. – Ian Turton Dec 04 '17 at 18:27
  • the two examples are using very different approaches and different functions. – JasonBK Dec 04 '17 at 18:43
0

this seems to work..simple variation of the first answer - this will retrieve and display attributes from multiple layers and from multiple features in each layer, if there are overlapping features

var layerlist = [wmsLayer, wmsLayer2];
var info = [];

map.on('singleclick', function(evt) {
  info = [];

for (i=0; i<layerlist.length; i++)
  getInfo(evt, layerlist[i]);
});

function getInfo(evt, layer) {
  var resolution = map.getView().getResolution();
  var coordinate = evt.coordinate;
  var pixel = evt.pixel;
  var thislayer = layer.getSource().getParams().LAYERS;


  var url = layer.getSource().getGetFeatureInfoUrl(evt.coordinate,
    resolution, 'EPSG:3857', {'INFO_FORMAT': 'application/json', 'FEATURE_COUNT': 50 });

if (url) {
      var parser = new ol.format.GeoJSON();
        var lookup = {};
       $.ajax({url: url,
          dataType: 'json',
          success:function(response) {
            var result = parser.readFeatures(response);
            if ((result.length > 0) && (wmsLayer.getVisible()==true)) {
              for (var i = 0, ii = result.length; i < ii; ++i) {
               var text = result[i].get("id");
               var param_name; 
               // to populate different field values from different layers in info popup
               if (thislayer == 'thisname' ){

                 param_name = result[i].get("param1");
               } else {
                 param_name = result[i].get("param2");
               }

                    info.push( param_name); 
              }
                content.innerHTML  = '<p><strong>ID: </strong>' + info.join(', ');


           overlay.setPosition(coordinate);
        }
      }
    });
  }
JasonBK
  • 539
  • 3
  • 12
  • 37