]> git.openstreetmap.org Git - rails.git/blobdiff - app/assets/javascripts/index/directions/fossgis_valhalla.js
Merge remote-tracking branch 'upstream/pull/5700'
[rails.git] / app / assets / javascripts / index / directions / fossgis_valhalla.js
index bc093fea47b642b2451b25fd0c3207eb74c2181b..41ad6a9725fcb332b3ec63030f36fad0f3c60343 100644 (file)
@@ -1,6 +1,6 @@
 (function () {
   function FOSSGISValhallaEngine(id, costing) {
-    var INSTR_MAP = [
+    const INSTR_MAP = [
       0, // kNone = 0;
       8, // kStart = 1;
       8, // kStartRight = 2;
       20 // kMergeLeft = 38;
     ];
 
-    return {
-      id: id,
-      creditline:
-      "<a href='https://gis-ops.com/global-open-valhalla-server-online/' target='_blank'>Valhalla (FOSSGIS)</a>",
-      draggable: false,
-
-      getRoute: function (points, callback) {
-        return $.ajax({
-          url: OSM.FOSSGIS_VALHALLA_URL,
-          data: {
-            json: JSON.stringify({
-              locations: points.map(function (p) {
-                return { lat: p.lat, lon: p.lng, radius: 5 };
-              }),
-              costing: costing,
-              directions_options: {
-                units: "km",
-                language: I18n.currentLocale()
-              }
-            })
-          },
-          dataType: "json",
-          success: function (data) {
-            var trip = data.trip;
+    function _processDirections(tripLegs) {
+      let line = [];
+      let steps = [];
+      let distance = 0;
+      let time = 0;
 
-            if (trip.status === 0) {
-              var line = [];
-              var steps = [];
-              var distance = 0;
-              var time = 0;
-
-              trip.legs.forEach(function (leg) {
-                var legLine = L.PolylineUtil.decode(leg.shape, {
-                  precision: 6
-                });
+      for (const leg of tripLegs) {
+        const legLine = L.PolylineUtil.decode(leg.shape, {
+          precision: 6
+        });
 
-                line = line.concat(legLine);
+        const legSteps = leg.maneuvers.map(function (manoeuvre, idx) {
+          const num = `<b>${idx + 1}.</b> `;
+          const lineseg = legLine
+            .slice(manoeuvre.begin_shape_index, manoeuvre.end_shape_index + 1)
+            .map(([lat, lng]) => ({ lat, lng }));
+          return [
+            lineseg[0],
+            INSTR_MAP[manoeuvre.type],
+            num + manoeuvre.instruction,
+            manoeuvre.length * 1000,
+            lineseg
+          ];
+        });
 
-                leg.maneuvers.forEach(function (manoeuvre, idx) {
-                  var point = legLine[manoeuvre.begin_shape_index];
+        line = line.concat(legLine);
+        steps = steps.concat(legSteps);
+        distance += leg.summary.length;
+        time += leg.summary.time;
+      }
 
-                  steps.push([
-                    { lat: point[0], lng: point[1] },
-                    INSTR_MAP[manoeuvre.type],
-                    "<b>" + (idx + 1) + ".</b> " + manoeuvre.instruction,
-                    manoeuvre.length * 1000,
-                    []
-                  ]);
-                });
+      return {
+        line: line,
+        steps: steps,
+        distance: distance * 1000,
+        time: time
+      };
+    }
 
-                distance = distance + leg.summary.length;
-                time = time + leg.summary.time;
-              });
+    return {
+      id: id,
+      creditline:
+      "<a href='https://gis-ops.com/global-open-valhalla-server-online/' target='_blank'>Valhalla (FOSSGIS)</a>",
+      draggable: false,
 
-              callback(false, {
-                line: line,
-                steps: steps,
-                distance: distance * 1000,
-                time: time
-              });
-            } else {
-              callback(true);
+      getRoute: function (points, signal) {
+        const query = new URLSearchParams({
+          json: JSON.stringify({
+            locations: points.map(function (p) {
+              return { lat: p.lat, lon: p.lng, radius: 5 };
+            }),
+            costing: costing,
+            directions_options: {
+              units: "km",
+              language: I18n.currentLocale()
             }
-          },
-          error: function () {
-            callback(true);
-          }
+          })
         });
+        return fetch(OSM.FOSSGIS_VALHALLA_URL + "?" + query, { signal })
+          .then(response => response.json())
+          .then(({ trip }) => {
+            if (trip.status !== 0) throw new Error();
+            return _processDirections(trip.legs);
+          });
       }
     };
   }