More draggable routes work
authorRichard Fairhurst <richard@systemeD.net>
Thu, 23 Jan 2014 11:38:01 +0000 (11:38 +0000)
committerRichard Fairhurst <richard@systemeD.net>
Thu, 23 Jan 2014 11:38:01 +0000 (11:38 +0000)
app/assets/javascripts/routing.js.erb
app/assets/javascripts/routing_engines/cloudmade_foot.js
app/assets/javascripts/routing_engines/mapquest_bicycle.js
app/assets/javascripts/routing_engines/osrm_car.js

index 81cd3e7a18f62250ac5abdfd5bf21c1570cccf44..0c11f93a06af0ba8546dc0a84e7b962f3149f86b 100644 (file)
@@ -56,6 +56,7 @@ OSM.Routing=function(map,name,jqSearch) {
        r.route_from=null;              // null=unset, false=awaiting response, [lat,lon]=geocoded
        r.route_to=null;                //  |
        r.awaitingGeocode=false;// true if the user has requested a route, but we're waiting on a geocode result
+       r.awaitingRoute=false;  // true if we've asked the engine for a route and are waiting to hear back
        r.viaPoints=[];                 // not yet used
 
        r.polyline=null;                // Leaflet polyline object
@@ -107,7 +108,7 @@ OSM.Routing=function(map,name,jqSearch) {
                r.updateMarkers(field.id);
                if (r.awaitingGeocode) {
                        r.awaitingGeocode=false;
-                       r.requestRoute();
+                       r.requestRoute(true);
                }
        };
 
@@ -118,6 +119,8 @@ OSM.Routing=function(map,name,jqSearch) {
                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[id.replace('marker','route')]=[ll.lat,ll.lng];
+               r.requestRoute(true);
                // update to/from field
        };
        r.createMarker=function(latlng,id) {
@@ -135,17 +138,20 @@ OSM.Routing=function(map,name,jqSearch) {
        r.markerDragged=function(e) {
                // marker has been dragged
                if (e.type=='drag' && !r.chosenEngine.draggable) return;
-               // *** also return if e.type=='drag' and not long enough since last request returned
-               // *** but always do if e.type=='dragend'
-               console.log(e.target.options.name);
-               console.log(e.target.getLatLng());
+               if (e.type=='drag' && r.awaitingRoute) return;
+               var id=e.target.options.name;
+               var ll=e.target.getLatLng();
+               r[id.replace('marker','route')]=[ll.lat,ll.lng];
+               r.requestRoute(e.type=='dragend');
+               // update to/from field
        };
        
        // Route-fetching UI
 
-       r.requestRoute=function() {
+       r.requestRoute=function(final) {
                if (r.route_from && r.route_to) {
-                       r.chosenEngine.getRoute(true,[r.route_from,r.route_to]);
+                       r.awaitingRoute=true;
+                       r.chosenEngine.getRoute(final,[r.route_from,r.route_to]);
                        // then, when the route has been fetched, it'll call the engine's gotRoute function
                } else if (r.route_from==false || r.route_to==false) {
                        // we're waiting for a Nominatim response before we can request a route
@@ -157,7 +163,8 @@ OSM.Routing=function(map,name,jqSearch) {
        r.setPolyline=function(line) {
                if (r.polyline) map.removeLayer(r.polyline);
                r.polyline=L.polyline(line, ROUTING_POLYLINE).addTo(r.map);
-               r.map.fitBounds(r.polyline.getBounds());
+               // r.map.fitBounds(r.polyline.getBounds());
+               // *** ^^^ we only want to do this for geocode-originated routes
        };
 
        // Take directions and write them out
@@ -222,7 +229,7 @@ OSM.Routing=function(map,name,jqSearch) {
                                script.src = url+r.name+".gotRoute"+num;
                                document.body.appendChild(script); 
                        };
-                       r['gotRoute'+num]=function(data) { list[num].gotRoute(r,data); };
+                       r['gotRoute'+num]=function(data) { r.awaitingRoute=false; list[num].gotRoute(r,data); };
                }
                select.append("<option value='"+i+"'>"+list[i].name+"</option>");
        }
index a2b7bbc9a80d2868cedf65bbee69e2bd3d2c18fe..7aa147e5cf60ef711dbd2660900bdb9608cc0c8e 100644 (file)
@@ -5,7 +5,7 @@
 
 OSM.RoutingEngines.list.push({
        name: 'Foot (CloudMade)',
-       draggable: true,
+       draggable: false,
        CM_SPRITE_MAP: {
                "C": 1,
                "TL": 7,
index bf33870782c034f3d2717a3772b4079eb012b5ae..2f7642de9e72bf625283e79712f26bc95d522d9a 100644 (file)
@@ -7,7 +7,7 @@
 
 OSM.RoutingEngines.list.push({
        name: 'Bicycle (MapQuest Open)',
-       draggable: true,
+       draggable: false,
        _hints: {},
        MQ_SPRITE_MAP: {
                0: 1, // straight
index 0fdb42889ce4d5e65ea4d37bc046857b00dc1b6d..e3eb6b44c0cc44b539efda866310dde42be473d4 100644 (file)
@@ -37,6 +37,6 @@ OSM.RoutingEngines.list.push({
                        if (instCodes[0]!=15) { instText+=s[1] ? "<b>"+s[1]+"</b>" : "(unnamed)"; }
                        steps.push([line[s[3]], s[0].split('-')[0], instText, s[2]]);
                }
-               router.setItinerary({ steps: steps });
+               if (steps.length) router.setItinerary({ steps: steps });
        }
 });