[gnome-maps] Keep user's last known location cached



commit 6e089809b8f3d148d86467a968de08b7aabb1b60
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Mon Apr 1 23:09:43 2013 +0300

    Keep user's last known location cached
    
    This will make it faster to go to user's location on startup and ensure
    we always go to some sane default if network or IP geocoding server is not
    available.

 data/org.gnome.maps.gschema.xml.in |   10 +++++++
 src/mapView.js                     |   49 +++++++++++++++++++++++++++---------
 2 files changed, 47 insertions(+), 12 deletions(-)
---
diff --git a/data/org.gnome.maps.gschema.xml.in b/data/org.gnome.maps.gschema.xml.in
index 8def1fc..f902d4d 100644
--- a/data/org.gnome.maps.gschema.xml.in
+++ b/data/org.gnome.maps.gschema.xml.in
@@ -15,5 +15,15 @@
       <_summary>Window maximized</_summary>
       <_description>Window maximized state</_description>
     </key>
+    <key name="last-location" type="ad">
+      <default>[40.71455, -74.007118]</default>
+      <_summary>Last known location</_summary>
+      <_description>Last known location of user (latitude and longitude in degrees).</_description>
+    </key>
+    <key name="last-location-description" type="s">
+      <default>"New York City, New York, USA"</default>
+      <_summary>Description of last known location</_summary>
+      <_description>Description of last known location of user.</_description>
+    </key>
   </schema>
 </schemalist>
diff --git a/src/mapView.js b/src/mapView.js
index 9afb683..0ef9184 100644
--- a/src/mapView.js
+++ b/src/mapView.js
@@ -28,6 +28,7 @@ const Geocode = imports.gi.GeocodeGlib;
 const Lang = imports.lang;
 const Mainloop = imports.mainloop;
 
+const Application = imports.application;
 const Utils = imports.utils;
 const _ = imports.gettext.gettext;
 
@@ -45,18 +46,7 @@ const MapView = new Lang.Class({
         this._markerLayer.set_selection_mode(Champlain.SelectionMode.SINGLE);
         this._view.add_layer(this._markerLayer);
 
-        let ipclient = new Geocode.Ipclient();
-        ipclient.server = "http://freegeoip.net/json/";;
-        ipclient.search_async(null, Lang.bind(this,
-            function(ipclient, res) {
-                try {
-                    let [location, accuracy] = ipclient.search_finish(res);
-
-                    this._gotoLocation(location, accuracy);
-                } catch (e) {
-                    log("Failed to find your location: " + e);
-                }
-            }));
+        this._gotoUserLocation();
     },
 
     geocodeSearch: function(string) {
@@ -91,6 +81,41 @@ const MapView = new Lang.Class({
             }));
     },
 
+    _gotoUserLocation: function () {
+        let lastLocation = Application.settings.get_value('last-location');
+        if (lastLocation.n_children() == 2) {
+            let lat = lastLocation.get_child_value(0);
+            let lng = lastLocation.get_child_value(1);
+
+            let location = new Geocode.Location({ latitude: lat.get_double(),
+                                                  longitude: lng.get_double() });
+            let lastLocationDescription = Application.settings.get_string('last-location-description');
+            location.set_description(lastLocationDescription);
+            // FIXME: We should keep the accuracy cached too but this type is soon going to change
+            //        from an enum to a double in geocode-glib so lets do it after that happens.
+            let accuracy = Geocode.LocationAccuracy.CITY;
+
+            this._gotoLocation(location, accuracy);
+        }
+
+        let ipclient = new Geocode.Ipclient();
+        ipclient.server = "http://freegeoip.net/json/";;
+        ipclient.search_async(null, Lang.bind(this,
+            function(ipclient, res) {
+                try {
+                    let [location, accuracy] = ipclient.search_finish(res);
+
+                    this._gotoLocation(location, accuracy);
+
+                    let variant = GLib.Variant.new('ad', [location.latitude, location.longitude]);
+                    Application.settings.set_value('last-location', variant);
+                    Application.settings.set_string('last-location-description', location.description);
+                } catch (e) {
+                    log("Failed to find your location: " + e);
+                }
+            }));
+    },
+
     _showLocations: function(locations) {
         if (locations.length == 0)
             return;


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