简体   繁体   English

Openlayers 5 在同一个 map 上为不同层显示不同的 Popup 信息

[英]Openlayers 5 Display different Popup informations for different layers on the same map

im using OpenLayers 5 to display two different layers on the same map.我使用OpenLayers 5在同一个 map 上显示两个不同的层。 I can see both Markers on the Map with different icons.我可以在 Map 上看到带有不同图标的两个标记。 The code below writes in the popup of one layer.下面的代码写在一层的弹出窗口中。 Now my question is: how can i display different Infos on the Popup for each specific layer.现在我的问题是:如何在每个特定图层的弹出窗口上显示不同的信息。 For example when the mouse is over the first icon the popup should contain the name of the first layer and when it is over the second different icon it shows the name of the second layer.例如,当鼠标悬停在第一个图标上时,弹出窗口应包含第一层的名称,而当鼠标悬停在第二个不同的图标上时,它会显示第二层的名称。

I assume i should use map.getFeaturesAtPixel(event.pixel, function (layer1)) or something like this but im facing problems right there我想我应该使用map.getFeaturesAtPixel(event.pixel, function (layer1))或类似的东西,但我在那里面临问题

//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);
    }
});

If you use forEachFeatureAtPixel use can add a layer filter function and use that to set the layer如果您使用forEachFeatureAtPixel使用可以添加一个图层过滤器 function 并使用它来设置图层

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);
    }

});

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

相关问题 显示具有多层的弹出窗口 - openlayers - Display popup with multiple layers - openlayers Openlayers:不同矢量图层上的标记 - Openlayers: Marker on different vector layers 来自两个不同来源的两个图层的 OpenLayers 地图缩放不同。 OpenLayers 可以改变缩放级别的行为吗? - OpenLayers map with two layers from two different sources zoom differently. Can OpenLayers change how zoom levels behave? 网站-几乎相同的客户,不同的信息 - Website - nearly same clients, different informations 如何在OpenLayers中区分两个不同的矢量层? - How can I distinguish two different vector layers in OpenLayers? OpenLayers4:Point位于地图上的不同位置 - OpenLayers4: Point is located in different location on map Openlayers - 在地图上的弹出窗口中显示来自 geojson 文件的数据 - Openlayers - Display data from geojson file in popup on map 使用两个不同的 js 都生成一个 openlayers map 每个都不会在同一个 div 中替换另一个 - With two different js both generating an openlayers map each does not replace the other in the same div 鼠标悬停d3.js与不同图层的映射 - Mouseover d3.js map with different layers 在Openlayers 3中使用WFS图层在弹出窗口上获取重叠的要素信息 - get overlapping features informations on popup using WFS layer in Openlayers 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM