3

I am using the leaflet API with OSM. I need to implement a functionality where on dragging the marker and dropping it on user preferred location, I need to get its latitude and longitude and save its location to database.

For now on double clicking the marker I am able to drag the marker. This line helped me:

draggable:true

Now when I release the marker I want to find the latitude and longitude of the location or invoking a function on dragging the marker, returning the lat,lng values.

2

1 Answer 1

11

Any draggable object will fire dragstart, dragend and other drag-related events.

Simply listen to the dragend events of the marker you marked as draggable, and subscribe a handler that gets the latlng value from the sent event:

marker.on('dragend', function(event) {
    var marker = event.target;  // you could also simply access the marker through the closure
    var result = marker.getLatLng();  // but using the passed event is cleaner
    console.log(result);
});
3
  • hi Matti, For some reason its giving me result as undefined...
    – DomincJune
    Nov 6, 2012 at 11:31
  • 1
    @DomincJune I had been too quick and had forgot to navigate the .target property of event. This should be fixed now.
    – MattiSG
    Nov 16, 2012 at 21:18
  • As a newbie, this post has been very useful along with this one on Leaflet Mailing List.
    – Inan
    Mar 6, 2013 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.