首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Openlayers 5显示同一地图上不同图层的不同弹出窗口信息

Openlayers 5显示同一地图上不同图层的不同弹出窗口信息
EN

Stack Overflow用户
提问于 2021-02-09 19:25:57
回答 1查看 116关注 0票数 0

我正在使用OpenLayers 5在同一张地图上显示两个不同的图层。我可以在地图上看到两个带有不同图标的标记。下面的代码写入了一个层的弹出窗口。现在我的问题是:如何在弹出窗口中为每个特定的层显示不同的信息。例如,当鼠标在第一个图标上时,弹出窗口应该包含第一层的名称,当鼠标在第二个不同的图标上时,它会显示第二层的名称。

我假设我应该使用map.getFeaturesAtPixel(event.pixel, function (layer1))或类似的东西,但我在那里遇到了问题。

代码语言:javascript
复制
//display the pop with on mouse over event
map.on('pointermove', function (event) {

    const features = map.getFeaturesAtPixel(event.pixel);
    
    if (features.length > 0 ) {  
        var coordinate = event.coordinate;
        
        //get the infos that are going to be displayed in the Pop-up window;
        const layerOneName = features[0].get('vehName');

        // text written inside the popup
        content.innerHTML = '<b>'+layerOneName +'</b>';
        overlay.setPosition(coordinate);
    }
});
EN

回答 1

Stack Overflow用户

发布于 2021-02-09 21:48:43

如果使用forEachFeatureAtPixel,则可以添加图层过滤函数并使用该函数来设置图层

代码语言:javascript
复制
map.on('pointermove', function (event) {

    let layer;
    const feature = map.forEachFeatureAtPixel(
      event.pixel,
      function (feature) {
        return feature;
      },
      { 
        layerFilter: function (candidate) {
          layer = candidate;
          return true;
        }
      }
    );
    
    if (feature) {  
        var coordinate = event.coordinate;
        
        //get the infos that are going to be displayed in the Pop-up window;
        const layerOneName = feature.get('vehName');

        // text written inside the popup
        content.innerHTML = '<b>'+layerOneName +'</b>';
        overlay.setPosition(coordinate);
    }

});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66118088

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档