0

我正在尝试使用 OpenLayers 3 和 Geoserver 提供的图层从地图单击上的多个图层中获取属性。

我找到了这个片段,但它使用的是旧版本的 Open Layers,我没有找到任何与 Open Layers 3 相同的东西

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

4

2 回答 2

0

OpenLayers 示例WMS GetFeatureInfo (Layers)正是展示了这一点。

<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>';
    }
  });
于 2017-12-02T10:05:36.117 回答
0

这似乎有效..第一个答案的简单变化 - 如果有重叠的特征,这将检索和显示来自多层和每层中的多个特征的属性

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);
        }
      }
    });
  }
于 2017-12-20T21:11:43.973 回答