[英]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.