传单在鼠标位置悬停时显示弹出窗口
Leaflet show popup on hover with the location of the mouse
我正在使用传单在地图上显示我的几何位置。现在我的弹出窗口工作正常,但是当您将鼠标悬停在它们上方时,弹出窗口的位置例如位于 line/string 的中间,而不是鼠标的位置。是否可以将其更改为鼠标的位置,这样地图就不会突然移动到不同的位置?
我用来打开传单中弹出窗口的代码如下:
function addPopup(feature, layer) {
var popupContent = feature.properties.name;
layer.bindPopup(popupContent);
layer.on('mouseover', function (e) {
this.openPopup();
});
layer.on('mouseout', function (e) {
this.closePopup();
});
}
您可以将鼠标点转换为经纬度并在那里设置弹出窗口。
layer.on('mouseover', function (e) {
var p = L.point([e.originalEvent.clientX,e.originalEvent.clientY])
var latlng = mymap.containerPointToLatLng(p);
this.openPopup(latlng)
});
layer.on('mousemove', function(e){
var p = L.point([e.originalEvent.clientX,e.originalEvent.clientY])
var latlng = mymap.containerPointToLatLng(p);
this.openPopup(latlng)
})
layer.on('mouseout', function (e) {
在@Falke Design 指出您可以将 latlng 坐标提供给 openPopup 函数后,我制作了一个更清晰的代码版本:
function addPopup(feature, layer) {
var popupContent = feature.properties.name;
layer.bindPopup(popupContent);
layer.on('mouseover', function (e) {
this.openPopup(e.latlng);
});
layer.on('mouseout', function (e) {
this.closePopup();
});
}
我正在使用传单在地图上显示我的几何位置。现在我的弹出窗口工作正常,但是当您将鼠标悬停在它们上方时,弹出窗口的位置例如位于 line/string 的中间,而不是鼠标的位置。是否可以将其更改为鼠标的位置,这样地图就不会突然移动到不同的位置?
我用来打开传单中弹出窗口的代码如下:
function addPopup(feature, layer) {
var popupContent = feature.properties.name;
layer.bindPopup(popupContent);
layer.on('mouseover', function (e) {
this.openPopup();
});
layer.on('mouseout', function (e) {
this.closePopup();
});
}
您可以将鼠标点转换为经纬度并在那里设置弹出窗口。
layer.on('mouseover', function (e) {
var p = L.point([e.originalEvent.clientX,e.originalEvent.clientY])
var latlng = mymap.containerPointToLatLng(p);
this.openPopup(latlng)
});
layer.on('mousemove', function(e){
var p = L.point([e.originalEvent.clientX,e.originalEvent.clientY])
var latlng = mymap.containerPointToLatLng(p);
this.openPopup(latlng)
})
layer.on('mouseout', function (e) {
在@Falke Design 指出您可以将 latlng 坐标提供给 openPopup 函数后,我制作了一个更清晰的代码版本:
function addPopup(feature, layer) {
var popupContent = feature.properties.name;
layer.bindPopup(popupContent);
layer.on('mouseover', function (e) {
this.openPopup(e.latlng);
});
layer.on('mouseout', function (e) {
this.closePopup();
});
}