繁体   English   中英

传单在鼠标位置悬停时显示弹出窗口

[英]Leaflet show popup on hover with the location of the mouse

我正在使用传单在地图上显示我的几何位置。 现在我的弹出窗口工作正常,但是当您将鼠标悬停在它们上方时,弹出窗口的位置例如位于行/字符串的中间,而不是鼠标的位置。 是否可以将其更改为鼠标的位置,以便地图不会突然移动到不同的位置?

我用来在传单中打开弹出窗口的代码如下:

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

您可以将鼠标点转换为 latlng 并在那里设置弹出窗口。

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 指出您可以为 openPopup 函数提供 latlng 坐标后,我制作了一个更简洁的代码版本:

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM