Given the following L.Latlng 's, L.Marker and L.Polyline :
var a = new L.LatLng(-45, -90), b = new L.LatLng(45, 0), c = new L.LatLng(-45, 90); var marker_a = new L.Marker(a, {draggable: true}).addTo(map), marker_b = new L.Marker(b, {draggable: true}).addTo(map), marker_c = new L.Marker(c, {draggable: true}).addTo(map); var polyline = new L.Polyline([a, b, c]).addTo(map);
You will need to attach eventlisteners and callbacks to your L.Marker . You can automate this, but I keep it simple:
marker_a .on('dragstart', dragStartHandler) .on('drag', dragHandler) .on('dragend', dragEndHandler); marker_b .on('dragstart', dragStartHandler) .on('drag', dragHandler) .on('dragend', dragEndHandler); marker_c .on('dragstart', dragStartHandler) .on('drag', dragHandler) .on('dragend', dragEndHandler);
Now on dragstart you will need to find latlng from the polyline that matches your latlng marker and save it in your marker instance so you can use it later:
function dragStartHandler (e) {
Now you know the key for the latlng polyline, which you can change when dragging the marker onto dragevent:
function dragHandler (e) {
Just to be clean and tidy, delete the saved key on dragend:
function dragEndHandler (e) {
What is it. Here is a working example on Plunker: http://embed.plnkr.co/SJRec3/preview