Categories
JavaScript Answers

How to listen for the click event for a marker in Leaflet?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *