[gnome-maps/wip/osrm-routing: 24/25] Route: split instructions and points



commit 83bda7ca942b5eb386b322798b13071ea2323534
Author: Mattias Bengtsson <mattias jc bengtsson gmail com>
Date:   Thu Jun 6 04:06:03 2013 +0200

    Route: split instructions and points
    
    Make the points in the suggested route polyline simple JSON-like
    objects and rename RoutePoint to Instruction.

 src/mapView.js |   31 +++++++++++------------
 src/osrm.js    |   75 +++++++++++++++++++++++++++----------------------------
 2 files changed, 52 insertions(+), 54 deletions(-)
---
diff --git a/src/mapView.js b/src/mapView.js
index 8f428f5..068e29e 100644
--- a/src/mapView.js
+++ b/src/mapView.js
@@ -117,22 +117,21 @@ const MapView = new Lang.Class({
             log("Got a " +route.length+ "m route with " + route.points.length + " nodes and " +
                 route.instructions.length + " turn instructions.");
 
+            route.points.forEach(function(point) {
+                this._routeLayer.add_node(new Champlain.Coordinate({
+                    latitude: point.lat,
+                    longitude: point.lon
+                }));
+            }, this);
 
-            for (let i = 0; i < route.points.length; i++) {
-                let coord = new Champlain.Coordinate();
-                coord.set_location(route.points[i]._lat,
-                                   route.points[i]._lon);
-                this._routeLayer.add_node(coord);
-            }
-
-            for (let i = 0; i < route.instructions.length; i++) {
-                let coord = new Champlain.Point();
-                coord.set_size(8.0);
-                coord.set_location(route.instructions[i]._lat,
-                                   route.instructions[i]._lon);
-                this._instructionsLayer.add_marker(coord);
-                log(" * " + route.instructions[i].getInstructionString());
-            }
+            route.instructions.forEach(function(instruction) {
+                this._instructionsLayer.add_marker(new Champlain.Point({
+                    latitude: instruction.point.lat,
+                    longitude: instruction.point.lon,
+                    size: 8.0
+                }));
+                log(" * " + instruction.toString());
+            }, this);
 
             this._routeLayer.visible = true;
             this._instructionsLayer.visible = true;
diff --git a/src/osrm.js b/src/osrm.js
index 8a25233..1d1c0da 100644
--- a/src/osrm.js
+++ b/src/osrm.js
@@ -92,26 +92,20 @@ const Status = {
     START_AND_END_POINTS_ARE_EQUAL: 210
 };
 
-const RoutePoint = new Lang.Class({
-    Name: 'RoutePoint',
-
-    _init: function(lat, lon) {
-        this._lat = lat;
-        this._lon = lon;
-    },
-
-    setInstructions: function(turnInstruction, dir, name, length, time) {
-        this._turnInstruction = turnInstruction;
-        this._wayName = name;
-        this._direction = dir;
-        this.length = length;
-        this._time = time;
+const Instruction = new Lang.Class({
+    Name: 'Instruction',
+
+    _init: function(point, data) {
+               this.point = point;
+               
+        this._turnInstruction = data.turnInstruction;
+        this._direction = data.direction;
+        this._length = data.length;
+        this._time = data.time;
+        this._wayName = data.wayName;
     },
 
-    getInstructionString: function() {
-        if (!this._turnInstruction)
-            return null;
-
+    toString: function() {
         let string;
         if (this._wayName && TurnInstruction[this._turnInstruction][1]) {
             string = TurnInstruction[this._turnInstruction][1];
@@ -121,7 +115,7 @@ const RoutePoint = new Lang.Class({
         }
         string = string.replace(/{DIR}/g, Direction[this._direction]);
 
-        return string + " (" + this.length + "m)";
+        return string + " (" + this._length + "m)";
     }
 });
 
@@ -151,16 +145,16 @@ const Route = new Lang.Class({
 
     _buildRoute: function(json) {
         let points = this._decodePolyline(json.route_geometry);
-        let instructions = this._applyInstructions(points, json.route_instructions);
+        let instructions = this._createInstructions(points, json.route_instructions);
         return [points, instructions];
     },
 
-    _applyInstructions: function(points, instructions) {
+    _createInstructions: function(points, instructionJSON) {
         if (!points)
             return [];
 
-        let instruction_points = [];
-        for (let i = 0; i < instructions.length; i++) {
+        let instructions = [];
+        instructionJSON.forEach(function(instruction) {
             // 0: turn instruction, see TurnInstruction
             // 1: way name
             // 2: length (m)
@@ -170,24 +164,26 @@ const Route = new Lang.Class({
             // 6: direction abbreviation
             // 7: azimuth
 
-            let point = points[instructions[i][3]];
+            let point = points[instruction[3]];
             if (!point) {
                 log("Turn instruction for non-existing point " +
-                    instructions[i][3]);
-                continue;
+                    instruction[3]);
+                return;
             }
-            if (!TurnInstruction[instructions[i][0]]) {
-                log("Unknown turn instruction " + instructions[i][0]);
-                continue;
+            if (!TurnInstruction[instruction[0]]) {
+                log("Unknown turn instruction " + instruction[0]);
+                return;
             }
-            point.setInstructions(instructions[i][0],
-                                  instructions[i][6],
-                                  instructions[i][1],
-                                  instructions[i][2],
-                                  instructions[i][4]);
-            instruction_points.push(point);
-        }
-        return instruction_points;
+
+            instructions.push(new Instruction(point, {
+                turnInstruction: instruction[0],
+                wayName: instruction[1],
+                length: instruction[2],
+                time: instruction[4],
+                direction: instruction[6]
+            }));
+        });
+        return instructions;
     },
 
     _decodeValue: function(data, index) {
@@ -228,7 +224,10 @@ const Route = new Lang.Class({
             // first value is absolute, rest are relative to previous value
             lat += latdelta;
             lon += londelta;
-            polyline.push(new RoutePoint(lat * 1e-5, lon * 1e-5));
+            polyline.push({
+                               lat: lat * 1e-5,
+                               lon: lon * 1e-5
+                       });
         }
         return polyline;
     }


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