leaflet.js - Set marker on click, update position on drag leaflet.js - Set marker on click, update position on drag jquery jquery

leaflet.js - Set marker on click, update position on drag


In the code snippet above, marker is not defined at the time the event handler is added. Try the following where the dragend listener is added immediately following the creation of the Marker:

function onMapClick(e) {    gib_uni();    marker = new L.marker(e.latlng, {id:uni, icon:redIcon, draggable:'true'});    marker.on('dragend', function(event){            var marker = event.target;            var position = marker.getLatLng();            console.log(position);            marker.setLatLng(position,{id:uni,draggable:'true'}).bindPopup(position).update();    });    map.addLayer(marker);};

You were also missing a bracket at the end of your new L.Marker() line.

You also put position into an array in the call to setLatLng but it is already a LatLng object.