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/

Roy
  • 1,468
  • 4
  • 21
  • 40

5 Answers5

22

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

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
Kedar.Aitawdekar
  • 2,364
  • 1
  • 23
  • 25
15

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

lintu
  • 1,092
  • 11
  • 24
  • 2
    How did you use the console to find that it was actually `e.target._latlng`? – Roy Aug 22 '13 at 15:45
  • put a debugger in fiddle – lintu Aug 22 '13 at 16:00
  • Give an example? I don't know how to console out a property I'm not aware of. – Roy Aug 22 '13 at 16:27
  • 9
    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.. https://developers.google.com/chrome-developer-tools/docs/javascript-debugging – lintu Aug 23 '13 at 14:14
  • 7
    It's better to use `e.target.getLatLng()`; that way we're not exposing any private variables. – user2441511 Nov 16 '16 at 20:35
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.

Community
  • 1
  • 1
user2441511
  • 11,036
  • 3
  • 25
  • 50
9

I think the API changed.

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

Aral Roca
  • 5,442
  • 8
  • 47
  • 78
  • 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](http://leafletjs.com/reference-1.0.3.html). On the other hand, `getLatLng()` is available for `L.marker`, `L.popup`, and `L.circleMarker`. – user2441511 Apr 24 '17 at 14:15
  • The API has not changed. In 1.3.0, for markers, this is still `e.target.getLatLng()`. – Matt Feb 28 '18 at 14:01
  • 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. – Pysis Apr 20 '18 at 22:40
0

if anyone is using react than you should use :

const map = useMap()
map.addEventListener("dragend" , ()=> {
const {lat , lng} = map.getCenter()
})
gouder hicham
  • 123
  • 10