- // Drag and drop markers
-
- r.handleDrop=function(e) {
- var id=e.originalEvent.dataTransfer.getData('id');
- var ll=r.map.mouseEventToLatLng(e.originalEvent);
- // *** ^^^ this is slightly off - we need to work out the latLng of the tip
- r.createMarker(ll,id);
- r.setNumericInput(ll,id);
- r.requestRoute(true);
- // update to/from field
- };
- r.createMarker=function(latlng,id) {
- if (r[id]) r.map.removeLayer(r[id]);
- r[id]=L.marker(latlng, {
- icon: id=='marker_from' ? icon_from : icon_to,
- draggable: true,
- name: id
- }).addTo(r.map);
- r[id].on('drag',r.markerDragged);
- r[id].on('dragend',r.markerDragged);
- };
- // Update marker from geocoded route input
- r.updateMarker=function(id) {
- var m=id.replace('route','marker');
- if (!r[m]) { r.createMarker(r[id],m); return; }
- var ll=r[m].getLatLng();
- if (ll.lat!=r[id][0] || ll.lng!=r[id][1]) {
- r.createMarker(r[id],m);
- }
- };
- // Marker has been dragged
- r.markerDragged=function(e) {
- if (e.type=='drag' && !r.chosenEngine.draggable) return;
- if (e.type=='drag' && r.awaitingRoute) return;
- r.setNumericInput(e.target.getLatLng(), e.target.options.name);
- r.requestRoute(e.type=='dragend');
- };
- // Set a route input field to a numeric value
- r.setNumericInput=function(ll,id) {
- var routeid=id.replace('marker','route');
- r[routeid]=[ll.lat,ll.lng];
- $("[name="+routeid+"]:visible").val(Math.round(ll.lat*10000)/10000+" "+Math.round(ll.lng*10000)/10000);
- }
-
- // Route-fetching UI
+ // Drag and drop markers
+
+ r.handleDrop=function(e) {
+ var oe=e.originalEvent;
+ var id=oe.dataTransfer.getData('id');
+ var pt=L.DomEvent.getMousePosition(oe,map.getContainer()); // co-ordinates of the mouse pointer at present
+ pt.x+=Number(oe.dataTransfer.getData('offsetX'));
+ pt.y+=Number(oe.dataTransfer.getData('offsetY'));
+ var ll=map.containerPointToLatLng(pt);
+ r.createMarker(ll,id);
+ r.setNumericInput(ll,id);
+ r.requestRoute(true, false);
+ // update to/from field
+ };
+ r.createMarker=function(latlng,id) {
+ if (r[id]) r.map.removeLayer(r[id]);
+ r[id]=L.marker(latlng, {
+ icon: id=='marker_from' ? icon_from : icon_to,
+ draggable: true,
+ name: id
+ }).addTo(r.map);
+ r[id].on('drag',r.markerDragged);
+ r[id].on('dragend',r.markerDragged);
+ };
+ // Update marker from geocoded route input
+ r.updateMarker=function(id) {
+ var m=id.replace('route','marker');
+ if (!r[m]) { r.createMarker(r[id],m); return; }
+ var ll=r[m].getLatLng();
+ if (ll.lat!=r[id][0] || ll.lng!=r[id][1]) {
+ r.createMarker(r[id],m);
+ }
+ };
+ // Marker has been dragged
+ r.markerDragged=function(e) {
+ r.dragging=(e.type=='drag'); // true for drag, false for dragend
+ if (r.dragging && !r.chosenEngine.draggable) return;
+ if (r.dragging && r.awaitingRoute) return;
+ r.setNumericInput(e.target.getLatLng(), e.target.options.name);
+ r.requestRoute(!r.dragging, false);
+ };
+ // Set a route input field to a numeric value
+ r.setNumericInput=function(ll,id) {
+ var routeid=id.replace('marker','route');
+ r[routeid]=[ll.lat,ll.lng];
+ $("[name="+routeid+"]:visible").val(Math.round(ll.lat*10000)/10000+" "+Math.round(ll.lng*10000)/10000);
+ }
+
+ // Route-fetching UI