How can I prevent popup showing by clik on marker in Leaflet?

心已入冬 提交于 2019-12-05 08:35:45

Just don't bind a popup to the marker. Here's a fiddle with 2 markers. One has a popup and the other does not.

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

L.marker([51, 1.5]).addTo(map);

EDIT: I've edited the fiddle and think it might be what you are asking. Here's the important part of the code:

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

Try this workaround:

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

See plunker for details.

None of the other answers worked for me (possibly because of a newer version of Leaflet). I ended up skipping marker.bindPopup() and just creating the popups separately using L.popup() then calling map.openPopup(thePopup) when I needed them to appear.

Something like this:

var map = L.map(),
    popup = L.popup().setContent("Stuff"),
    marker = L.marker();
popup.setLatLng(marker.getLatLng());

// In my event handler
map.openPopup(popup);

Juste remove openPopup from the click event of the marker.

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

add return false . it should work. though i am not very sure.

marker.on('click', function(event) {
  event.originalEvent.preventDefault();
  return false;
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!