简体   繁体   English

OpenLayers 3-几个WMS图层,如何仅从可见图层获取功能信息?

[英]OpenLayers 3 - Several WMS layers, how to get Feature Info only from visible ones?

I have an Openlayers map with several WMS layers from which I want to request feature information through "getGetFeatureInfoUrl". 我有一个Openlayers地图,其中包含几个WMS图层,我想通过这些地图通过“ getGetFeatureInfoUrl”请求要素信息。 Visibility of layers can be turned on/off in the layer tree. 可以在层树中打开/关闭层的可见性。 I'd like to, upon clicking somewhere in the map: 我想点击地图上的某个位置:

  • Get Feature Info ONLY for layers that are currently visible 仅获取当前可见图层的要素信息
  • and, if there are more than one layers at the chosen location, get the Feature Info for all of them. 并且,如果在所选位置有多个图层,则获取所有图层的要素信息。

I used the sample code from the OpenLayers website. 我使用了OpenLayers网站上的示例代码。 I tried variants of this code bit 我尝试了此代码位的变体

var url = layers[2].getSource().getGetFeatureInfoUrl(
    evt1.coordinate, viewResolution, 'EPSG:3857', {
        'INFO_FORMAT': 'text/html',
            'FEATURE_COUNT': '300'
    });

like 喜欢

var url = layers[].getSource().getGetFeatureInfoUrl( or var url = layers[1,2].getSource().getGetFeatureInfoUrl( , but either no Feature Info is delivered, or merely for the last layer - regardless of whether it is visible or not. var url = layers[].getSource().getGetFeatureInfoUrl(var url = layers[1,2].getSource().getGetFeatureInfoUrl( ,但要么不传递任何要素信息,要么仅传递最后一层-无论是否传递是否可见。

I created a JSFiddle with two sample layers here: http://jsfiddle.net/kidalex/j34xzaa3/5/ 我在这里创建了带有两个示例层的JSFiddle: http : //jsfiddle.net/kidalex/j34xzaa3/5/

Similar questions were asked before, like here: https://gis.stackexchange.com/questions/114297/querying-multiple-wms-layers-in-ol3-and-adding-to-a-single-popup-window ; 之前曾问过类似的问题,例如: https : //gis.stackexchange.com/questions/114297/querying-multiple-wms-layers-in-ol3-and-adding-to-a-single-popup-window but I cannot fathom how to apply the solutions (JS/OL newbie here). 但我无法理解如何应用解决方案(此处为JS / OL新手)。

You should iterate over your layers and only call getFeatureInfo if they are visible and not the base layer, try something like: 您应该遍历各层,并且仅在可见时调用getFeatureInfo,而不是基础层,请尝试以下操作:

map.on('singleclick', function (evt1) {
    document.getElementById('info').innerHTML = '';
    var viewResolution = /** @type {number} */
    (view.getResolution());
    var url = '';
    document.getElementById('info').innerHTML ='';
    layers.forEach(function (layer, i, layers) {
        if (layer.getVisible() && layer.get('name')!='Basemap') {
            url = layer.getSource().getGetFeatureInfoUrl(evt1.coordinate, viewResolution, 'EPSG:3857', {
                'INFO_FORMAT': 'text/html',
                    'FEATURE_COUNT': '300'
            });
            if (url) {
                document.getElementById('info').innerHTML +=
                    '<iframe seamless src="' + url + '"></iframe>';
            }
        }
    });

});

EDIT: grammar 编辑:语法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM