[gnome-maps] route: Add via points support



commit e18119674be19918f9abc23670ec4ec43113c510
Author: Dario Di Nucci <linkin88mail gmail com>
Date:   Sun Jul 20 11:14:35 2014 +0200

    route: Add via points support
    
    Add support for adding, removing and modifying
    via points in route.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=731068

 src/routeQuery.js   |  114 ++++++++++++++++++++++++++++-----------------------
 src/routeService.js |   24 +++++------
 2 files changed, 74 insertions(+), 64 deletions(-)
---
diff --git a/src/routeQuery.js b/src/routeQuery.js
index 58769c9..bf6ad2b 100644
--- a/src/routeQuery.js
+++ b/src/routeQuery.js
@@ -46,19 +46,14 @@ const RouteQuery = new Lang.Class({
     Name: 'RouteQuery',
     Extends: GObject.Object,
     Signals: {
-        'updated': { }
+        'reset': { }
     },
     Properties: {
-        'from': GObject.ParamSpec.object('from',
-                                         '',
-                                         '',
-                                         GObject.ParamFlags.READWRITE,
-                                         Geocode.Place),
-        'to': GObject.ParamSpec.object('to',
-                                       '',
-                                       '',
-                                       GObject.ParamFlags.READWRITE,
-                                       Geocode.Place),
+        'points': GObject.ParamSpec.object('points',
+                                            '',
+                                            '',
+                                            GObject.ParamFlags.READWRITE,
+                                            GObject.Object),
         'transportation': GObject.ParamSpec.int('transportation',
                                                 '',
                                                 '',
@@ -68,68 +63,85 @@ const RouteQuery = new Lang.Class({
                                                 Transportation.CAR)
     },
 
-    set from(place) {
-        this._from = place;
-        this.notify("from");
-    },
-    get from() {
-        return this._from;
+    get points() {
+        return this._points;
     },
 
-    set to(place) {
-        this._to = place;
-        this.notify("to");
-    },
-    get to() {
-        return this._to;
+    set points(points) {
+        this._points = points;
+        this.notify('points');
     },
 
-    set transportation(transportation) {
-        this._transportation = transportation;
-        this.notify("transportation");
-    },
-    get transportation() {
-        return this._transportation;
+    get filledPoints() {
+        return this.points.filter(function(point) {
+            return point.place
+        });
     },
 
     _init: function(args) {
         this.parent(args);
-        this._connectSignals();
+        this._points = [];
         this.reset();
     },
 
-    _connectSignals: function() {
-        this._updatedId = this.connect('notify', (function() {
-            this.emit('updated');
+    addPoint: function(point, index) {
+        this._points.splice(index, 0, point);
+        point.connect('notify::place', (function() {
+            this.notify('points');
         }).bind(this));
     },
 
-    _disconnectSignals: function() {
-        this.disconnect(this._updatedId);
+    removePoint: function(index) {
+        this._points.splice(index, 1);
+        this.notify('points');
+    },
+
+    set transportation(transportation) {
+        this._transportation = transportation;
+        this.notify('transportation');
+    },
+    get transportation() {
+        return this._transportation;
     },
 
     reset: function() {
-        this.setMany({ from: null,
-                       to: null,
-                       transportation: Transportation.CAR });
+        this._transportation = Transportation.CAR;
+        this._points.forEach(function(point) {
+            point.place = null;
+        });
+        this.emit('reset');
     },
 
-    setMany: function(obj) {
-        this._disconnectSignals();
+    isValid: function() {
+        if (this.filledPoints.length >= 2)
+            return true;
+        else
+            return false;
+    },
 
-        // Only set properties actually defined on this object
-        ["from", "to", "transportation"].forEach((function(prop) {
-            if (obj.hasOwnProperty(prop))
-                this[prop] = obj[prop];
-        }).bind(this));
+    toString: function() {
+        return "\nPoints: " + this.points +
+               "\nTransportation: " + this.transportation;
+    }
+});
+
+const QueryPoint = new Lang.Class({
+    Name: 'QueryPoint',
+    Extends: GObject.Object,
+    Properties: {
+        'place': GObject.ParamSpec.object('place',
+                                          '',
+                                          '',
+                                          GObject.ParamFlags.READWRITE,
+                                          Geocode.Place),
+    },
 
-        this._connectSignals();
-        this.emit('updated');
+    set place(p) {
+        this._place = p;
+        this.notify('place');
     },
 
-    toString: function() {
-        return "From: " + this.from +
-            "\nTo: " + this.to +
-            "\nTransportation" + this.transportation;
+    get place() {
+        return this._place;
     }
 });
diff --git a/src/routeService.js b/src/routeService.js
index 42a8113..73f2a60 100644
--- a/src/routeService.js
+++ b/src/routeService.js
@@ -54,26 +54,24 @@ const GraphHopper = new Lang.Class({
         this._route   = new Route.Route();
         this._query   = new RouteQuery.RouteQuery();
 
-        this.query.connect('updated', (function() {
-            if (this.query.from && this.query.to) {
-                this.fetchRoute([this.query.from.location,
-                                 this.query.to.location],
-                                this.query.transportation);
-            } else
-                this.route.reset();
+        this.query.connect('notify', (function() {
+            if (this.query.isValid())
+                this.fetchRoute(this.query.filledPoints,
+                                this._query.transportation);
         }).bind(this));
 
         this.parent();
     },
 
-    fetchRoute: function(viaPoints, transportationType) {
-        let url = this._buildURL(viaPoints, transportationType);
+    fetchRoute: function(points, transportationType) {
+        let url = this._buildURL(points, transportationType);
         let msg = Soup.Message.new('GET', url);
         this._session.queue_message(msg, (function(session, message) {
             try {
                 let result = this._parseMessage(message);
                 if (!result) {
                     Application.notificationManager.showMessage(_("No route found."));
+                    this.route.reset();
                 } else {
                     let route = this._createRoute(result.paths[0]);
                     this.route.update(route);
@@ -85,16 +83,16 @@ const GraphHopper = new Lang.Class({
         }).bind(this));
     },
 
-    _buildURL: function(viaPoints, transportation) {
-        let points = viaPoints.map(function({ latitude, longitude }) {
-            return [latitude, longitude].join(',');
+    _buildURL: function(points, transportation) {
+        let locations = points.map(function(point) {
+            return [point.place.location.latitude, point.place.location.longitude].join(',');
         });
         let vehicle = RouteQuery.Transportation.toString(transportation);
         let query = new HTTP.Query({ type:    'json',
                                      key:     this._key,
                                      vehicle: vehicle,
                                      locale:  this._locale,
-                                     point:   points,
+                                     point:   locations,
                                      debug:   Utils.debugEnabled
                                    });
         let url = this._baseURL + query.toString();


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