Using Leaflet Js, is it possible to know the onclick location of a Marker (ignore anchor point)?
On one hand: whenever Leaflet handles a mouse (or touch) event, you can access the original DOM event in the originalEvent
property of the event.
On the other hand: Given a mouse (or touch) DOM event, Leaflet can magically translate its clientX
and clientY
properties into an instance of L.LatLng
by using map.mouseEventToLatLng()
.
Combine these two things together, and you can have something like:
marker.on('click', function(ev){
var latlng = map.mouseEventToLatLng(ev.originalEvent);
console.log(latlng.lat + ', ' + latlng.lng);
});
Check Leaflet's documentation for the other conversion methods, as they might prove useful.
You always can retrieve the coordinates from the Leaflet object map. You can use something like this:
map.on('click', function(e){
var coord = e.latlng;
var lat = coord.lat;
var lng = coord.lng;
console.log("You clicked the map at latitude: " + lat + " and longitude: " + lng);
});
Here you have a working example.