[GIS] Showing popup on mouse-over, not on click using Leaflet

leafletpopup

Is it possible in Leaflet that popup opens on mouse-over, not on click?

This is working for just one marker at a time, but I need it for a bigger number of markers:

marker.on('mouseover', function(e){
    marker.openPopup();
});

Best Answer

If you need to show the popup for a marker you can use markers bindPopup method.

Then you have more control and it will automatically be bound to your marker.

In the example below you can show the popup when the user mouses over, and hide it when the user mouses out:

        marker.bindPopup("Popup content");
        marker.on('mouseover', function (e) {
            this.openPopup();
        });
        marker.on('mouseout', function (e) {
            this.closePopup();
        });

Note: You may run into issues with the popups closing when you mouse onto the popup itself, so you might need to adjust the popup anchor in (see popup settings) to show your popups a bit farther away from marker itself so it doesn't disappear too easily.