[gnome-maps/wip/mlundblad/transit-plugin-resrobot: 1/2] graphHopperTransit: Add function to add walking instructions



commit afe20ff41a899c8a6f891e771c3bace093fc3d50
Author: Marcus Lundblad <ml update uu se>
Date:   Sat Oct 12 23:48:29 2019 +0200

    graphHopperTransit: Add function to add walking instructions
    
    Add function to add walking instructions to walking legs
    of existing itineraries.

 src/graphHopperTransit.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
---
diff --git a/src/graphHopperTransit.js b/src/graphHopperTransit.js
index 27bb620..6f09745 100644
--- a/src/graphHopperTransit.js
+++ b/src/graphHopperTransit.js
@@ -105,3 +105,49 @@ function createQueryPointForCoord(coord) {
     point.place = place;
     return point;
 }
+
+function addWalkingToItineraries(itineraries, callback) {
+    _addWalkingToItinerariesRecursive(itineraries, 0, callback);
+}
+
+function _addWalkingToItinerariesRecursive(itineraries, index, callback) {
+    if (index === itineraries.length) {
+        callback();
+    } else {
+        let itinerary = itineraries[index];
+
+        _addWalkingToLegsRecursive(itinerary.legs, 0, () => {
+            _addWalkingToItinerariesRecursive(itineraries, index + 1, callback);
+        });
+    }
+}
+
+function _addWalkingToLegsRecursive(legs, index, callback) {
+    if (index === legs.length) {
+        callback();
+    } else {
+        let leg = legs[index];
+
+        if (!leg.transit) {
+            let from = createQueryPointForCoord(leg.fromCoordinate);
+            let to = createQueryPointForCoord(leg.toCoordinate);
+
+            fetchWalkingRoute([from, to], (route) => {
+                if (route) {
+                    let duration = route.time / 1000;
+
+                    if (index === 0 || index === legs.length - 1 ||
+                        duration <= leg.duration) {
+                        leg.distance = route.distance;
+                        leg.walkingInstructions = route.turnPoints;
+                        leg.polyline = route.path;
+                    }
+                }
+
+                _addWalkingToLegsRecursive(legs, index + 1, callback);
+            });
+        } else {
+            _addWalkingToLegsRecursive(legs, index + 1, callback);
+        }
+    }
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]