Leafletjs dragg market only one axis
1 answer
You can use events dragstart
and drag
to handle this. You keep the marker position while dragging and then reset the latitude the entire time the event is emitted drag
.
//Marker position before dragging
var latLngBeforeDrag;
//On drag start
function onDragStart(e){
var marker = e.target;
latLngBeforeDrag = marker.getLatLng();
}
//On drag
function onDrag(e){
var marker = e.target;
marker.setLatLng(L.latLng(latLngBeforeDrag.lat, marker.getLatLng().lng));
}
//Create new marker
var marker = L.marker([36.83711,-2.464459], {draggable:true});
marker.on('dragstart', onDragStart);
marker.on('drag', onDrag);
//Add marker to map
marker.addTo(map);
Example: jsfiddle
+3
source to share