Use generic GraphHopper engine
authorRichard Fairhurst <richard@systemeD.net>
Sat, 8 Mar 2014 12:31:06 +0000 (12:31 +0000)
committerRichard Fairhurst <richard@systemeD.net>
Sat, 8 Mar 2014 12:31:06 +0000 (12:31 +0000)
app/assets/javascripts/routing.js.erb
app/assets/javascripts/routing_engines/graphhopper.js [new file with mode: 0644]
app/assets/javascripts/routing_engines/graphhopper_bicycle.js [deleted file]
config/locales/en.yml

index d1c66e2939b6d86d98983fc584e54f1f018e2f54..2a10a9d5e571cd8025ea30e6cfad0160af31bef5 100644 (file)
@@ -238,7 +238,7 @@ OSM.Routing=function(map,name,jqSearch) {
 
        // Add all engines
        var list=OSM.RoutingEngines.list;
-       list.sort(function(a,b) { return a.name>b.name; });
+       list.sort(function(a,b) { return I18n.t(a.name)>I18n.t(b.name); });
        var select=r.jqSearch.find('select.routing_engines');
        for (var i=0; i<list.length; i++) {
                // Set up JSONP callback
diff --git a/app/assets/javascripts/routing_engines/graphhopper.js b/app/assets/javascripts/routing_engines/graphhopper.js
new file mode 100644 (file)
index 0000000..9762c0f
--- /dev/null
@@ -0,0 +1,63 @@
+GraphHopperEngine = function(vehicleName, vehicleParam, locale) {
+    this.vehicleName = vehicleName;
+    this.vehicleParam = vehicleParam;
+    this.locale = locale;
+    if (!locale)
+        this.locale = "en";
+};
+
+GraphHopperEngine.prototype.createConfig = function() {
+    var that = this;
+    return {
+        name: "javascripts.directions.engines.graphhopper_"+this.vehicleName.toLowerCase(),
+        draggable: false,
+        _hints: {},
+        getRoute: function(isFinal, points) {
+            var url = "http://graphhopper.com/routing/api/route?" 
+                    + that.vehicleParam 
+                    + "&locale=" + that.locale;
+            for (var i = 0; i < points.length; i++) {
+                var pair = points[i].join(',');
+                url += "&point=" + pair;
+            }            
+            if (isFinal)
+                url += "&instructions=true";
+            // GraphHopper supports json too
+            this.requestJSONP(url + "&type=jsonp&callback=");
+        },
+        gotRoute: function(router, data) {
+            if (!data.info.routeFound) {
+                return false;
+            }
+            // Draw polyline
+            var line = L.PolylineUtil.decode(data.route.coordinates);
+            router.setPolyline(line);
+            // Assemble instructions
+            var steps = [];
+            var instr = data.route.instructions;
+            for (i = 0; i < instr.descriptions.length; i++) {
+                var indi = instr.indications[i];
+                var instrCode = (i == instr.descriptions.length - 1) ? 15 : this.GH_INSTR_MAP[indi];
+                var instrText = "<b>" + (i + 1) + ".</b> ";
+                instrText += instr.descriptions[i];
+                var latlng = instr.latLngs[i];
+                var distInMeter = instr.distances[i];
+                steps.push([{lat: latlng[0], lng: latlng[1]}, instrCode, instrText, distInMeter]);
+            }
+            router.setItinerary({steps: steps});
+            return true;
+        },
+        GH_INSTR_MAP: {
+            "-3": 6, // sharp left
+            "-2": 7, // left   
+            "-1": 8, // slight left                
+            0: 0, // straight
+            1: 1, // slight right
+            2: 2, // right
+            3: 3 // sharp right                
+        }
+    };
+};
+
+OSM.RoutingEngines.list.push(new GraphHopperEngine("Bicycle", "vehicle=bike").createConfig());
+OSM.RoutingEngines.list.push(new GraphHopperEngine("Foot", "vehicle=foot").createConfig());
diff --git a/app/assets/javascripts/routing_engines/graphhopper_bicycle.js b/app/assets/javascripts/routing_engines/graphhopper_bicycle.js
deleted file mode 100644 (file)
index acf0892..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-// GraphHopper bicycle engine
-
-OSM.RoutingEngines.list.push({    
-    name: "javascripts.directions.engines.graphhopper_bike",
-    creditline: 'Directions courtesy of <a href="http://graphhopper.com/routing/" target="_blank">Graphhopper</a>',
-    draggable: true,
-    _hints: {},
-    getRoute: function(isFinal, points) {
-        var url = "http://graphhopper.com/routing/api/route?vehicle=bike&locale=" + I18n.currentLocale();
-        for (var i = 0; i < points.length; i++) {
-            var pair = points[i].join(',');
-            url += "&point=" + pair;
-        }
-        if (isFinal)
-            url += "&instructions=true";
-        this.requestJSONP(url + "&type=jsonp&callback=");
-    },
-    gotRoute: function(router, data) {
-        if (!data.info.routeFound) {
-            return false;
-        }
-        // Draw polyline
-        var line = L.PolylineUtil.decode(data.route.coordinates);
-        router.setPolyline(line);
-        // Assemble instructions
-        var steps = [];
-        var instr = data.route.instructions;
-        for (i = 0; i < instr.descriptions.length; i++) {
-            var indi = instr.indications[i];
-            var instrCode = (i==instr.descriptions.length-1) ? 15 : this.GH_INSTR_MAP[indi];
-            var instrText = "<b>" + (i + 1) + ".</b> ";
-            instrText += instr.descriptions[i];
-            var latlng = instr.latLngs[i];
-            var distInMeter = instr.distances[i];            
-            steps.push([{lat: latlng[0], lng: latlng[1]}, instrCode, instrText, distInMeter]);
-        }
-        router.setItinerary({steps: steps});
-        return true;
-    },
-    GH_INSTR_MAP: {
-        "-3": 6, // sharp left
-        "-2": 7, // left       
-        "-1": 8, // slight left                
-        0: 0, // straight
-        1: 1, // slight right
-        2: 2, // right
-        3: 3 // sharp right            
-    }
-});
index 46fe41d10d9a0d626c8f61fc5ef68e14fc31fbac..8cb9c967453cfbd234795cbde79ce6e152aafbb3 100644 (file)
@@ -2120,7 +2120,8 @@ en:
     edit_help: Move the map and zoom in on a location you want to edit, then click here.
     directions:
       engines:
-        graphhopper_bike: "Bicycle (GraphHopper)"
+        graphhopper_bicycle: "Bicycle (GraphHopper)"
+        graphhopper_foot: "Foot (GraphHopper)"
         mapquest_bike: "Bicycle (MapQuest)"
         osrm_car: "Car (OSRM)"
         cloudmade_foot: "Foot (Cloudmade)"