Sometimes, we want to listen for the click event for a marker in Leaflet.
In this article, we’ll look at how to listen for the click event for a marker in Leaflet.
How to listen for the click event for a marker in Leaflet?
To listen for the click event for a marker in Leaflet, we can use the on
method.
For instance, we write
L.marker([10.496093, -66.881935])
.addTo(map)
.on("click", (e) => {
console.log(e.latlng);
});
to call L.marker
with an array with the longitude and latitude to create a marker.
Then wee call addTo
to add it to the map
.
And then we call on
to add a click listener.
In the click listener, we get the latitude and longitude with e.latlng
.
Conclusion
To listen for the click event for a marker in Leaflet, we can use the on
method.