- r.geocode=function(id,event) { var _this=this;
- var field=event.target;
- var v=event.target.value;
- // *** do something if v==''
- var querystring = '<%= NOMINATIM_URL %>search?q=' + encodeURIComponent(v) + '&format=json';
- // *** &accept-language=<%#= request.user_preferred_languages.join(',') %>
- // *** prefer current viewport
- r[field.id]=false;
- $.getJSON(querystring, function(json) { _this._gotGeocode(json,field); });
- };
-
- r._gotGeocode=function(json,field) {
- if (json.length==0) {
- alert("Sorry, couldn't find that place."); // *** internationalise
- r[field.id]=null;
- return;
- }
- field.value=json[0].display_name;
- var lat=Number(json[0].lat), lon=Number(json[0].lon);
- r[field.id]=[lat,lon];
- r.updateMarker(field.id);
- if (r.awaitingGeocode) {
- r.awaitingGeocode=false;
- r.requestRoute(true);
- }
- };
+ // 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