简体   繁体   English

如何 select 在 OpenLayers 中重叠或相同几何图形的情况下具有覆盖功能

[英]How to select which feature overlay in case of overlapping or same geometry in OpenLayers

I created a map in which an overlay is opened clicking on the features.我创建了一个 map,其中单击功能打开了一个覆盖。 But I have problem if more than one feature are in overlapping, or worse, when features have the same geometry.但是,如果多个要素重叠,或者更糟糕的是,当要素具有相同的几何形状时,我会遇到问题。

I use this code to show the overlay.我使用此代码来显示叠加层。 But when two features have the same geometry only one overlay is shown.但是,当两个要素具有相同的几何图形时,只会显示一个叠加层。

const overlayContainerElement = document.querySelector('.overlay-container');

    const overlayLayer = new ol.Overlay ({
        element: overlayContainerElement
    });
    map.addOverlay(overlayLayer);
    const overlayFeatureName = document.getElementById('feature-name');
    const overlayFeatureAdditionalinfo = document.getElementById('feature-additionalinfo');


    map.on('click', function(e){
        overlayLayer.setPosition(undefined);
        map.forEachFeatureAtPixel(e.pixel, function (feature, layer){
            let clickedCoordinate = e.coordinate;
            let clickedFeatureName = feature.get('Number')
            let clickedFeatureInfo = feature.get('Text');
            overlayLayer.setPosition(clickedCoordinate);
            overlayFeatureName.innerHTML = clickedFeatureName;
            overlayFeatureAdditionalinfo.innerHTML = clickedFeatureInfo;
        },
        {
            layerFilter: function(layerCandidate){
                if (layerCandidate.get('title') === "Title") {
                    return 1;
                }
            }
        })
    });

Any idea how to configure a way in order to select which feature detail I want to see?知道如何配置一种方式以便 select 我想查看哪些功能细节? So after clicking, if two features are under the mouse, a sort of list is shown to select what overlay I want to open.所以点击后,如果鼠标下有两个功能,会显示一个列表到 select 我想打开什么叠加层。

Instead of updating your overlay inside the map.forEachFeatureAtPixel method, you could use the map.forEachFeatureAtPixel method to collect all features that were clicked, and then work with an Array of features .您可以使用map.forEachFeatureAtPixel方法收集所有被点击的特征,然后使用Array of features ,而不是更新map.forEachFeatureAtPixel方法中的叠加层。

Something like this (untested / unfinished):像这样的东西(未经测试/未完成):

        var features = [];
        map.forEachFeatureAtPixel(e.pixel, function (feature, layer){
          features.push(feature);
        },
        {
            layerFilter: function(layerCandidate){
                if (layerCandidate.get('title') === "Title") {
                    return 1;
                }
            }
        });

        // And then, you could iterate on your features and show information as a list in your overlay.

        // todo - loop here
        /*
            let clickedCoordinate = e.coordinate;
            let clickedFeatureName = feature.get('Number')
            let clickedFeatureInfo = feature.get('Text');
            overlayLayer.setPosition(clickedCoordinate);
            overlayFeatureName.innerHTML = clickedFeatureName;
            overlayFeatureAdditionalinfo.innerHTML = clickedFeatureInfo;
        */

You could have your overlay show a list of results in a "summary" fashion, and clicking on one of the feature name (for example) could show its full details within the overlay.您可以让您的叠加层以“摘要”方式显示结果列表,然后单击其中一个功能名称(例如)可以在叠加层中显示其完整详细信息。

HTH高温高压

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

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