Search code examples
javascriptleaflet

Marker in leaflet, click event


var map = L.map('map');
var marker = L.marker([10.496093,-66.881935]).on('click', onClick);
function onClick(e) {alert(e.latlng);}
marker.addTo(map)

When I do click in the marker, the alert message is: undefined

But if I put it in the variable map, it works! (shows latitude and longitude)

map.on('click', onClick); 

Does someone know why it doesn't work in the marker?


Solution

  • I found the solution:

    function onClick(e) {
       console.log(this.getLatLng());
    }
    

    used the method getLatLng() of the marker