Using GraphHopper new API
authorPeter <peathal@yahoo.de>
Tue, 1 Apr 2014 14:22:06 +0000 (16:22 +0200)
committerPeter <peathal@yahoo.de>
Tue, 1 Apr 2014 14:22:06 +0000 (16:22 +0200)
app/assets/javascripts/routing_engines/graphhopper.js

index 0f29c16..4758336 100644 (file)
@@ -15,9 +15,13 @@ GraphHopperEngine.prototype.createConfig = function() {
         draggable: false,
         _hints: {},
         getRoute: function(isFinal, points) {
-            var url = "http://graphhopper.com/routing/api/route?" 
-                    + that.vehicleParam 
-                    + "&locale=" + I18n.currentLocale();
+            // documentation
+            // https://github.com/graphhopper/graphhopper/blob/master/docs/web/api-doc.md
+            var url = "http://graphhopper.com/api/1/route?" 
+                    + that.vehicleParam
+                    + "&locale=" + I18n.currentLocale()
+                    + "&key=LijBPDQGfu7Iiq80w3HzwB4RUDJbMbhs6BU0dEnn";
+            
             for (var i = 0; i < points.length; i++) {
                 var pair = points[i].join(',');
                 url += "&point=" + pair;
@@ -28,25 +32,26 @@ GraphHopperEngine.prototype.createConfig = function() {
             this.requestJSONP(url + "&type=jsonp&callback=");
         },
         gotRoute: function(router, data) {
-            if (!data.info.routeFound) {
+            if (!data.paths || data.paths.length == 0)
                 return false;
-            }
+            
             // Draw polyline
-            var line = L.PolylineUtil.decode(data.route.coordinates);
+            var path = data.paths[0];
+            var line = L.PolylineUtil.decode(path.points);
             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 len = path.instructions.length;
+            for (i = 0; i < len; i++) {
+                var instr = path.instructions[i];                
+                var instrCode = (i === len - 1) ? 15 : this.GH_INSTR_MAP[instr.sign];
                 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, []]); // TODO does graphhopper map instructions onto line indices?
+                instrText += instr.text;
+                var latLng = line[instr.interval[0]];                
+                var distInMeter = instr.distance;
+                steps.push([{lat: latLng.lat, lng: latLng.lng}, instrCode, instrText, distInMeter, []]); // TODO does graphhopper map instructions onto line indices?
             }
-            router.setItinerary({ steps: steps, distance: data.route.distance, time: data.route['time']/1000 });
+            router.setItinerary({ steps: steps, distance: path.distance, time: path.time / 1000 });
             return true;
         },
         GH_INSTR_MAP: {
@@ -56,7 +61,9 @@ GraphHopperEngine.prototype.createConfig = function() {
             0: 0, // straight
             1: 1, // slight right
             2: 2, // right
-            3: 3 // sharp right                
+            3: 3, // sharp right
+            4: -1, // finish reached
+            5: -1 // via reached
         }
     };
 };