Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want a popup that doesn't show itself when I click on the Leaflet marker. I cannot use clickable : false because it will make the markers "act as a part of the underlying map" and this is unacceptable for me. I tried the following code:

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

without any results. What is the right way to prevent a popup from showing without using the clickable : false property of the marker object?

All I need is to open all the popups on the map by clicking on one custom button, but I don't want the popups to show themselves after I click on a particular marker.

like image 600
Anton Kasianchuk Avatar asked Oct 29 '14 18:10

Anton Kasianchuk


3 Answers

Juste remove openPopup from the click event of the marker.

marker.bindPopup('My popup!');
marker.off('click', this.openPopup);
like image 96
Shiva127 Avatar answered Sep 23 '22 07:09

Shiva127


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();
}
like image 29
pk. Avatar answered Sep 24 '22 07:09

pk.


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.

like image 44
mattesCZ Avatar answered Sep 20 '22 07:09

mattesCZ