]> git.openstreetmap.org Git - rails.git/blobdiff - app/assets/javascripts/index/directions/graphhopper.js
Merge remote-tracking branch 'upstream/pull/5621'
[rails.git] / app / assets / javascripts / index / directions / graphhopper.js
index 356de34778a588ce44ba786fa533ccfc55d08fa7..19147587303b69b079fbc3792c526a64b6f0193a 100644 (file)
@@ -1,74 +1,88 @@
-function GraphHopperEngine(id, vehicleParam) {
-  var 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
-    4: -1, // finish reached
-    5: -1 // via reached
-  };
+(function () {
+  function GraphHopperEngine(id, vehicleType) {
+    const GH_INSTR_MAP = {
+      "-3": 7, // sharp left
+      "-2": 6, // left
+      "-1": 5, // slight left
+      "0": 0, // straight
+      "1": 1, // slight right
+      "2": 2, // right
+      "3": 3, // sharp right
+      "4": 14, // finish reached
+      "5": 14, // via reached
+      "6": 10, // roundabout
+      "-7": 19, // keep left
+      "7": 18, // keep right
+      "-98": 4, // unknown direction u-turn
+      "-8": 4, // left u-turn
+      "8": 4 // right u-turn
+    };
 
-  return {
-    id: id,
-    creditline: '<a href="https://graphhopper.com/" target="_blank">Graphhopper</a>',
-    draggable: false,
+    function _processDirections(path) {
+      const line = L.PolylineUtil.decode(path.points);
 
-    getRoute: function (points, callback) {
-      // documentation
-      // https://github.com/graphhopper/graphhopper/blob/master/docs/web/api-doc.md
-      var url = document.location.protocol + "//graphhopper.com/api/1/route?"
-        + vehicleParam
-        + "&locale=" + I18n.currentLocale()
-        + "&key=LijBPDQGfu7Iiq80w3HzwB4RUDJbMbhs6BU0dEnn"
-        + "&type=jsonp"
-        + "&instructions=true";
-
-      for (var i = 0; i < points.length; i++) {
-        url += "&point=" + points[i].lat + ',' + points[i].lng;
-      }
+      const steps = path.instructions.map(function (instr, i) {
+        const num = `<b>${i + 1}.</b> `;
+        const lineseg = line
+          .slice(instr.interval[0], instr.interval[1] + 1)
+          .map(([lat, lng]) => ({ lat, lng }));
+        return [
+          lineseg[0],
+          GH_INSTR_MAP[instr.sign],
+          num + instr.text,
+          instr.distance,
+          lineseg
+        ]; // TODO does graphhopper map instructions onto line indices?
+      });
+      steps.at(-1)[1] = 14;
 
-      $.ajax({
-        url: url,
-        dataType: 'jsonp',
-        success: function (data) {
-          if (!data.paths || data.paths.length == 0)
-            return callback(true);
+      return {
+        line: line,
+        steps: steps,
+        distance: path.distance,
+        time: path.time / 1000,
+        ascend: path.ascend,
+        descend: path.descend
+      };
+    }
 
-          var path = data.paths[0];
-          var line = L.PolylineUtil.decode(path.points);
+    return {
+      id: id,
+      creditline: "<a href=\"https://www.graphhopper.com/\" target=\"_blank\">GraphHopper</a>",
+      draggable: false,
 
-          var steps = [];
-          var len = path.instructions.length;
-          for (var i = 0; i < len; i++) {
-            var instr = path.instructions[i];
-            var instrCode = (i === len - 1) ? 15 : GH_INSTR_MAP[instr.sign];
-            var instrText = "<b>" + (i + 1) + ".</b> ";
-            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?
+      getRoute: function (points, callback) {
+        // GraphHopper Directions API documentation
+        // https://graphhopper.com/api/1/docs/routing/
+        const data = {
+          vehicle: vehicleType,
+          locale: I18n.currentLocale(),
+          key: "LijBPDQGfu7Iiq80w3HzwB4RUDJbMbhs6BU0dEnn",
+          elevation: false,
+          instructions: true,
+          turn_costs: vehicleType === "car",
+          point: points.map(p => p.lat + "," + p.lng)
+        };
+        return $.ajax({
+          url: OSM.GRAPHHOPPER_URL,
+          data,
+          traditional: true,
+          dataType: "json",
+          success: function ({ paths }) {
+            if (!paths || paths.length === 0) {
+              return callback(true);
+            }
+            callback(false, _processDirections(paths[0]));
+          },
+          error: function () {
+            callback(true);
           }
+        });
+      }
+    };
+  }
 
-          callback(null, {
-            line: line,
-            steps: steps,
-            distance: path.distance,
-            time: path.time / 1000
-          });
-        }
-      });
-    }
-  };
-}
-
-OSM.Directions.addEngine(GraphHopperEngine("graphhopper_bicycle", "vehicle=bike"), true);
-OSM.Directions.addEngine(GraphHopperEngine("graphhopper_foot", "vehicle=foot"), true);
+  OSM.Directions.addEngine(new GraphHopperEngine("graphhopper_car", "car"), true);
+  OSM.Directions.addEngine(new GraphHopperEngine("graphhopper_bicycle", "bike"), true);
+  OSM.Directions.addEngine(new GraphHopperEngine("graphhopper_foot", "foot"), true);
+}());