为什么弹出窗口没有在传单标记中第二次显示

Why the popup is not showing second time in leaflet marker

我正在我的 leaflet 地图中绘制一个 标记,单击该标记时我会显示一条弹出消息。

如果我第一次单击标记,我会看到弹出消息。但是,如果我关闭弹出消息,然后 再次单击 标记,我看不到弹出消息,尽管在打印控制台消息时代码进入了点击事件代码块。

这是我的点击事件代码

circle.on("click",function(ev){
    var velocity=this.options.speed;
    console.log(velocity.toFixed(2));
    var layer=ev.target;
    layer.bindPopup('Speed: '+velocity.toFixed(2));
    console.log("Where is pop");
    layer.openPopup();
});

目前,每次用户单击标记时,您都会创建弹出窗口。可能这会造成问题。

您只需使用一次 bindPopup() 函数,即当您创建 标记时 。并且只能在 click 函数中使用 openPopup()。试试这个

//Place below two lines where you create the marker
var velocity=this.options.speed; //you might need to change this line to get the speed value
circle.bindPopup('Speed: '+velocity.toFixed(2));

//open the popup when user click the marker
circle.on("click",function(ev){
    layer.openPopup();
});