【问题标题】:openlayers 3 wms getfeatureinfo popupopenlayers 3 wms getfeatureinfo 弹出窗口
【发布时间】:2015-11-13 15:29:57
【问题描述】:

我想在我的网页中添加一个带有 WMS 图层的 OSM 地图,其中包含一些关于图层的信息。对我来说最好的方法是使用 getFeatureInfoUrl 请求创建弹出窗口,但不幸的是我没有这样做的经验。我尝试了很多代码和教程,但都不起作用。

我是这样写的:

var osm = new ol.layer.Tile({
	source: new ol.source.OSM()
});
var wms = new ol.layer.Tile({
	source: new ol.source.TileWMS(({
	url: 'http://localhost:8081/geoserver/KORTOWO/wms',
	params: {'LAYERS': 'roads', 'TILED': "true"},
	serverType: 'geoserver',
 })),
 title: "Roads"
});


var map = new ol.Map({
    target: 'map',
    layers: [osm,wms],
    view: new ol.View({
        center: ol.proj.transform([20.45, 53.75], 'EPSG:4326', 'EPSG:3857'),
        zoom: 14
    })
});


var popup = new ol.Overlay.Popup();
map.addOverlay(popup);

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

您能帮我修改代码以使其正常工作吗?我正在使用 OpenLayers3。

您好, 卡罗琳娜

【问题讨论】:

    标签: javascript popup openlayers-3 wms getfeatureinfo


    【解决方案1】:

    更新 2

    将其包装在一个函数中将是(未测试):

    map.on('singleclick', function(evt) {
        var layerWithWmsSource = map.forEachLayerAtPixel(evt.pixel, 
                function(layer) {
            // return only layers with ol.source.TileWMS
            var source = layer.getSource();
            if (source instanceof ol.source.TileWMS) {
                return layer;
            }
        });
        if (layerWithWmsSource) {
            getInfo(evt, layerWithWmsSource);
        }
    });
    
    function getInfo(evt, layer) {
      var resolution = map.getView().getResolution();
      var url = layer.getSource().getGetFeatureInfoUrl(evt.coordinate, 
        resolution, 'EPSG:3857', {'INFO_FORMAT': 'text/html'});
      if (url) {
        var content = '<p>' + url + '</p>';
        popup.show(evt.coordinate, content);
      }
    }
    

    更新getGetFeatureInfoUrl()ol.source.TileWMS的方法,所以修改为:

    var url = wms.getSource().getGetFeatureInfoUrl(evt.coordinate, resolution,
            'EPSG:3857', {'INFO_FORMAT': 'text/html'});
    

    也许你可以通过这些修改来解决:

    map.on('singleclick', function(evt) {
      var resolution = map.getView().getResolution();
    
      var url = wms.getSource().getGetFeatureInfoUrl(evt.coordinate, resolution,
        'EPSG:3857', {'INFO_FORMAT': 'text/html'});
    
      if (url) {
        // maybe you don't want|need an <iframe> inside popup
        var content = '<iframe seamless src="' + url + '"></iframe>';
        popup.show(evt.coordinate, content);
      } else {
        // maybe you hide the popup here
        popup.hide();
      }
    });
    

    【讨论】:

    • 不幸的是什么也没发生 :( 我有信息, wms.getFeatureInfoUrl 不是一个函数...
    • 注意,也许你不想在弹出窗口中出现&lt;iframe&gt;
    • 非常感谢,它有效! :) 还有一个问题:如果我想将此代码用于多个层怎么办?我应该复制它并在 url 部分更改图层的名称还是有一些更简单的方法?
    • 记住,这只对ol.source.TileWMS有效,所以如果你不止一个,你可以把它包装在一个函数中。如果这是您的情况,请告诉我。
    • 实际上我想添加多个 wms 层。你能帮我把它写成一个函数吗?
    猜你喜欢
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多