我正在使用OpenLayers 5在同一张地图上显示两个不同的图层。我可以在地图上看到两个带有不同图标的标记。下面的代码写入了一个层的弹出窗口。现在我的问题是:如何在弹出窗口中为每个特定的层显示不同的信息。例如,当鼠标在第一个图标上时,弹出窗口应该包含第一层的名称,当鼠标在第二个不同的图标上时,它会显示第二层的名称。
我假设我应该使用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);
}
});发布于 2021-02-09 21:48:43
如果使用forEachFeatureAtPixel,则可以添加图层过滤函数并使用该函数来设置图层
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);
}
});https://stackoverflow.com/questions/66118088
复制相似问题