getFeatureInfo仅针对露天层中的可见层查询6

发布于 2025-02-08 16:55:06 字数 2014 浏览 3 评论 0原文

我是JS和OL的Newby,并且我有一个OpenLayers 6 Map,其中不同的WMS层将不同的WMS分组为几个ol.layer.group。我想通过“ GetGetFeatureInfourl”索取功能信息。可以在图层树中打开/关闭图层的可见性。我想单击地图中的某个地方时:

  • 仅获取目前可见的层的功能信息
  • ,如果所选位置有多个层以上的层,请获取所有这些代码的功能信息,

此代码适用于只有一层(在这种情况下为WMSLAYER5),

map.on('singleclick', function (evt) {
if (!wmsLayer5.getVisible()) return;
document.getElementById('info').innerHTML = '';
const viewResolution = /** @type {number} */ (vista.getResolution());
const url = wmsLayer5.getSource().getFeatureInfoUrl(
    evt.coordinate,
    viewResolution,
    'EPSG:25830',
    {'INFO_FORMAT': 'text/html'}
    );
    if (url) {
        fetch(url)
        .then((response) => response.text())
        .then((html) => {
            document.getElementById('info').innerHTML = html;
        });
    }
});

但是我需要一个代码来迭代所有层,并且仅在可见(如果可见)时只能致电GetFeatureInfo。我已经尝试过这个,但不起作用,也不返回控制台中的任何消息

map.on('singleclick', function (evt1) {
            document.getElementById('info').innerHTML = '';
            var viewResolution = /** @type {number} */
            (vista.getResolution());
            var url = '';
            document.getElementById('info').innerHTML ='';
            layers.forEach(function (layer, i, layers) {
                if (layer.getVisible() ) {
                    url = layer.getSource().getGetFeatureInfoUrl(
                        evt1.coordinate, 
                        viewResolution, 
                        'EPSG:25830', {
                        'INFO_FORMAT': 'text/html',
                            'FEATURE_COUNT': '300'
                    });
                    if (url) {
                        fetch(url)
                        .then((response) => response.text())
                        .then((html) => {
                            document.getElementById('info').innerHTML += html;
                        });
                    }
                }
            });
        
        });

来修复它?

I'm a newby with JS and OL, and I have an Openlayers 6 map with different WMS layers grouped into several ol.layer.Group. I want to request feature information through "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

This code works well for just one layer (wmsLayer5, in this case)

map.on('singleclick', function (evt) {
if (!wmsLayer5.getVisible()) return;
document.getElementById('info').innerHTML = '';
const viewResolution = /** @type {number} */ (vista.getResolution());
const url = wmsLayer5.getSource().getFeatureInfoUrl(
    evt.coordinate,
    viewResolution,
    'EPSG:25830',
    {'INFO_FORMAT': 'text/html'}
    );
    if (url) {
        fetch(url)
        .then((response) => response.text())
        .then((html) => {
            document.getElementById('info').innerHTML = html;
        });
    }
});

But I need a code to iterate over all layers and only call getFeatureInfo if they are visible. I've tried this one, but doesn't work and doesn't return any message in the console

map.on('singleclick', function (evt1) {
            document.getElementById('info').innerHTML = '';
            var viewResolution = /** @type {number} */
            (vista.getResolution());
            var url = '';
            document.getElementById('info').innerHTML ='';
            layers.forEach(function (layer, i, layers) {
                if (layer.getVisible() ) {
                    url = layer.getSource().getGetFeatureInfoUrl(
                        evt1.coordinate, 
                        viewResolution, 
                        'EPSG:25830', {
                        'INFO_FORMAT': 'text/html',
                            'FEATURE_COUNT': '300'
                    });
                    if (url) {
                        fetch(url)
                        .then((response) => response.text())
                        .then((html) => {
                            document.getElementById('info').innerHTML += html;
                        });
                    }
                }
            });
        
        });

Any suggestion to fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舟遥客 2025-02-15 16:55:06

自6.13版(4个月前)以来,此方法已被弃用,并且在下一个主要版本之前不会更改。我没有使用建议的替换layer.getData()。按照代码迭代所有层和查询仅可见图像和类型图像。



    map.on("click", onMouseClick);
    function onMouseClick(browserEvent) {
        let coordinate = browserEvent.coordinate;
        let pixel = map.getPixelFromCoordinate(coordinate);
        map.forEachLayerAtPixel(pixel, function (layer) {
            if (layer instanceof ol.layer.Image) {
                if (layer.get("visible")) {
                    let url = layer.getSource().getFeatureInfoUrl(coordinate, map.getView().getResolution(), "EPSG:3857", {
                        INFO_FORMAT: "application/json",
                    });
                    if (url) {
                        fetch(url)
                            .then(function (response) {
                                return response.text();
                            })
                            .then(function (json) {
                                let data = JSON.parse(json);
                                if (data.features.length > 0) {
                                    //Do something with data.features
                                }
                            });
                    }
                }
            }
        });
    }

This method has been deprecated since version 6.13 (4 months ago) and won't be changed before next major release. I didn't use suggested replacement layer.getData(). Following code iterates through all the layers and queries only visible ones and of type Image.



    map.on("click", onMouseClick);
    function onMouseClick(browserEvent) {
        let coordinate = browserEvent.coordinate;
        let pixel = map.getPixelFromCoordinate(coordinate);
        map.forEachLayerAtPixel(pixel, function (layer) {
            if (layer instanceof ol.layer.Image) {
                if (layer.get("visible")) {
                    let url = layer.getSource().getFeatureInfoUrl(coordinate, map.getView().getResolution(), "EPSG:3857", {
                        INFO_FORMAT: "application/json",
                    });
                    if (url) {
                        fetch(url)
                            .then(function (response) {
                                return response.text();
                            })
                            .then(function (json) {
                                let data = JSON.parse(json);
                                if (data.features.length > 0) {
                                    //Do something with data.features
                                }
                            });
                    }
                }
            }
        });
    }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文