How do I get the latlng after the dragend event in leaflet?
Asked Answered
E

5

24

I'm trying to update the lat/lng value of a marker after it is moved. The example provided uses a popup window to display the lat/lng.

I have a "dragend" event listener for the marker, but when I alert the value of e.latlng it returns undefined.

javascript:

function markerDrag(e){
    alert("You dragged to: " + e.latlng);
}

function initialize() {
    // Initialize the map
    var map = L.map('map').setView([38.487, -75.641], 8);
    L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
        attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>',
        maxZoom: 18
    }).addTo(map);

    // Event Handlers
    map.on('click', function(e){
        var marker = new L.Marker(e.latlng, {draggable:true});
        marker.bindPopup("<strong>"+e.latlng+"</strong>").addTo(map);

        marker.on('dragend', markerDrag);
    });
}


$(document).ready(initialize());

http://jsfiddle.net/rhewitt/Msrpq/4/

Ernaline answered 22/8, 2013 at 14:12 Comment(0)
A
15

latlng value is not in e.latlng but in e.target._latlng . Use console.

Archduke answered 22/8, 2013 at 14:41 Comment(5)
How did you use the console to find that it was actually e.target._latlng?Ernaline
put a debugger in fiddleArchduke
Give an example? I don't know how to console out a property I'm not aware of.Ernaline
write "debugger"; where you want to break and open the console.(reload the page :) ) . Once the break point is hit, hover over the property or type in console. You can see all the contents. this will help.. developers.google.com/chrome-developer-tools/docs/…Archduke
It's better to use e.target.getLatLng(); that way we're not exposing any private variables.Ambo
C
24

Use e.target.getLatLng() to get the latlng of the updated position.

// Script for adding marker on map click
function onMapClick(e) {

    var marker = L.marker(e.latlng, {
             draggable:true,
             title:"Resource location",
             alt:"Resource Location",
             riseOnHover:true
            }).addTo(map)
              .bindPopup(e.latlng.toString()).openPopup();

    // #12 : Update marker popup content on changing it's position
    marker.on("dragend",function(e){

        var changedPos = e.target.getLatLng();
        this.bindPopup(changedPos.toString()).openPopup();

    });
}

JSFiddle demo

Clamor answered 23/3, 2014 at 19:42 Comment(3)
The key piece of this that answers the OP's question is ev.target.getLatLng().Beaconsfield
Is there anyway for folium, I can't find this answer for folium.Somatist
This is what I want, but on folium.Somatist
A
15

latlng value is not in e.latlng but in e.target._latlng . Use console.

Archduke answered 22/8, 2013 at 14:41 Comment(5)
How did you use the console to find that it was actually e.target._latlng?Ernaline
put a debugger in fiddleArchduke
Give an example? I don't know how to console out a property I'm not aware of.Ernaline
write "debugger"; where you want to break and open the console.(reload the page :) ) . Once the break point is hit, hover over the property or type in console. You can see all the contents. this will help.. developers.google.com/chrome-developer-tools/docs/…Archduke
It's better to use e.target.getLatLng(); that way we're not exposing any private variables.Ambo
A
10

While using e.target._latlng works (as proposed by this other answer), it's better practice to use

e.target.getLatLng();

That way we're not exposing any private variables, as is recommended by Leaflet:

Private properties and methods start with an underscore (_). This doesn’t make them private, just recommends developers not to use them directly.

Ambo answered 16/11, 2016 at 20:38 Comment(2)
e.target.getLatLng is not a functionKurgan
BTW, const { lat, lng } = e.target.getCenter(); is working perfectly for meKurgan
K
10

I think the API changed.

Nowadays is: const { lat, lng } = e.target.getCenter();

Kurgan answered 22/4, 2017 at 8:47 Comment(3)
What object are you clicking on and what version of Leaflet? I only see the getCenter() method available for L.map, L.latLngBounds and L.polyline for Leaflet 1.0.3. On the other hand, getLatLng() is available for L.marker, L.popup, and L.circleMarker.Ambo
The API has not changed. In 1.3.0, for markers, this is still e.target.getLatLng().Beaconsfield
This helped! On Leaflet version 1.0.3 here. No getLatLng method. I got as far to looking in the console and found various L.Point classes, but this was the data I wanted. Even in the source version of Leaflet, I only saw the target object as NewClass, which didn't help for looking through the docs online, and I also did not see much in the way of a "move" event that I was using.Keener
O
0

if anyone is using react than you should use :

const map = useMap()
map.addEventListener("dragend" , ()=> {
const {lat , lng} = map.getCenter()
})
Ossieossietzky answered 3/9, 2022 at 1:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.