Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
1  
Downvote: bad formatting, no respect for “no please in questions” guidelines, but most of all most basic research not done. –  MattiSG Nov 5 '12 at 18:30
    

1 Answer 1

up vote 6 down vote accepted

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);
});
share|improve this answer
    
hi Matti, For some reason its giving me result as undefined... –  DomincJune Nov 6 '12 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 '12 at 21:18
    
As a newbie, this post has been very useful along with this one on Leaflet Mailing List. –  Inan Berbatov Mar 6 '13 at 17:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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