单击传单中的标记时,如何防止弹出窗口显示

How can I prevent a popup to show when clicking on a marker in Leaflet?

本文关键字:何防止 显示 窗口 单中 单击      更新时间:2023-09-26

我想要一个弹出窗口,当我点击传单标记时不会显示出来。我不能使用CCD_ 1,因为它将使标记";充当底层地图的一部分";这对我来说是不可接受的。我尝试了以下代码:

marker.on('click', function(event) {
  event.originalEvent.preventDefault();
});

没有任何结果。在不使用标记对象的clickable : false属性的情况下,防止弹出窗口显示的正确方法是什么?

我只需要点击一个自定义按钮打开地图上的所有弹出窗口,但我不希望在点击特定标记后弹出窗口显示出来。

只是不要将弹出窗口绑定到标记。这是一把有两个记号的小提琴。一个有弹出窗口,另一个没有。

L.marker([51, 0]).bindPopup("this is a popup").addTo(map);
L.marker([51, 1.5]).addTo(map);

编辑:我已经编辑了小提琴,认为这可能是你所要求的。以下是代码的重要部分:

function onClick(event) {
    event.target.closePopup();
}

尝试此解决方法:

marker.bindPopup('my popup content');
// remove openPopup click handler (actually all click handlers)
marker.off('click');
// Do nothing on click. This is not necessary, but now marker
// doesn't act like part of underlying map
marker.on('click', function() {return;});

详见plunker。

其他答案对我都不起作用(可能是因为传单的更新版本)。我最终跳过了marker.bindPopup(),只使用L.popup()单独创建弹出窗口,然后在需要它们出现时调用map.openPopup(thePopup)

类似这样的东西:

var map = L.map(),
    popup = L.popup().setContent("Stuff"),
    marker = L.marker();
popup.setLatLng(marker.getLatLng());
// In my event handler
map.openPopup(popup);

Juste从标记的点击事件中删除openPopup

marker.bindPopup('My popup!');
marker.off('click', this.openPopup);

不需要自己删除事件侦听器,只要标记转换到您的"被禁用";状态:

marker.bindPopup('My popup!');
marker.unbindPopup();

添加return false。它应该起作用。虽然我不太确定。

marker.on('click', function(event) {
  event.originalEvent.preventDefault();
  return false;
});