[gnome-clocks/wip/geolocation] Restrict location search within the same country, if possible



commit 14a3641c1a15b9871b9154bab389a2d14e670ea9
Author: Evgeny Bobkin <evgen ibqn gmail com>
Date:   Tue Aug 27 13:58:45 2013 +0200

    Restrict location search within the same country, if possible

 src/geolocation-info.vala    |    1 +
 src/geolocation-monitor.vala |    4 ++++
 src/geolocation-utils.vala   |   30 +++++++++++++++++++++++++-----
 src/world.vala               |    3 +--
 4 files changed, 31 insertions(+), 7 deletions(-)
---
diff --git a/src/geolocation-info.vala b/src/geolocation-info.vala
index 3790eb9..f1abd65 100644
--- a/src/geolocation-info.vala
+++ b/src/geolocation-info.vala
@@ -49,6 +49,7 @@ public class LocationInfo : GLib.Object {
             
     public LocationAccuracy accuracy { get; set; default = LocationAccuracy.UNKNOWN; }
     public string? description { get; set; default = null; }
+    public string? country_name { get; set; default = null; }
     public uint64 timestamp { get; set; default = 0; }
     
     public LocationInfo (double lat, double lon, LocationAccuracy acc = LocationAccuracy.UNKNOWN) {
diff --git a/src/geolocation-monitor.vala b/src/geolocation-monitor.vala
index 7171d0c..9f110c2 100644
--- a/src/geolocation-monitor.vala
+++ b/src/geolocation-monitor.vala
@@ -62,6 +62,10 @@ public class LocationMonitor : GLib.Object {
         
         location.description = parse_description (root_object);
         
+        if (root_object.has_member ("country_name")) {
+            location.country_name = root_object.get_string_member ("country_name");
+        }
+
         return location;
     }
 
diff --git a/src/geolocation-utils.vala b/src/geolocation-utils.vala
index 8ae6cd5..85fd94a 100644
--- a/src/geolocation-utils.vala
+++ b/src/geolocation-utils.vala
@@ -15,17 +15,27 @@ private double get_distance (double latitude1, double longitude1, double latitud
     return Math.acos (Math.cos (lat1) * Math.cos (lat2) * Math.cos (lon1 - lon2) + Math.sin (lat1) * 
Math.sin (lat2)) * radius;
 }
 
-private void search_locations_helper (double lat, double lon, GWeather.Location location, ref double 
minimal_distance,  ref GWeather.Location? found_location) {
+private void search_locations_helper (GeoInfo.LocationInfo geo_location, GWeather.Location location, ref 
double minimal_distance,  ref GWeather.Location? found_location) {
     GWeather.Location? [] locations = location.get_children ();
 
     if (locations != null) {
         for (int i = 0; i < locations.length; i++) {
             if (locations[i].get_level () == GWeather.LocationLevel.CITY) {
                 if (locations[i].has_coords ()) {
+                    if (geo_location.country_name != null) {
+                        stdout.printf ("geo name %s\n", geo_location.country_name);
+                        string? country_name = get_country_name (location);
+                        if (country_name != null) {
+                            if (country_name != geo_location.country_name) {
+                                continue;
+                            }
+                        }
+                    }
+
                     double latitude, longitude, distance;
 
                     locations[i].get_coords (out latitude, out longitude);
-                    distance = get_distance (lat, lon, latitude, longitude);
+                    distance = get_distance (geo_location.latitude, geo_location.longitude, latitude, 
longitude);
 
                     if (distance < minimal_distance) {
                         found_location = locations[i];
@@ -34,17 +44,27 @@ private void search_locations_helper (double lat, double lon, GWeather.Location
                 }
             }
 
-            search_locations_helper (lat, lon, locations[i], ref minimal_distance, ref found_location);
+            search_locations_helper (geo_location, locations[i], ref minimal_distance, ref found_location);
         }
     }
 }
 
-public GWeather.Location? search_locations (double lat, double lon) {
+private string? get_country_name (GWeather.Location location) {
+     var nation = location;
+
+     while (nation != null && nation.get_level () != GWeather.LocationLevel.COUNTRY) {
+        nation = nation.get_parent ();
+     }
+
+     return nation != null ? nation.get_name () : null;
+}
+
+public GWeather.Location? search_locations (GeoInfo.LocationInfo geo_location) {
     GWeather.Location locations = new GWeather.Location.world (true);
     GWeather.Location? found_location = null;
     double minimal_distance = 1000.0d;
 
-    search_locations_helper (lat, lon, locations, ref minimal_distance, ref found_location);
+    search_locations_helper (geo_location, locations, ref minimal_distance, ref found_location);
 
     return found_location;
 }
diff --git a/src/world.vala b/src/world.vala
index 7336d21..946394e 100644
--- a/src/world.vala
+++ b/src/world.vala
@@ -360,8 +360,7 @@ public class MainPanel : Gtk.Stack, Clocks.Clock {
         try {
             geo_location = yield monitor.search ();
 
-            found_location = GeoInfo.Utils.search_locations (geo_location.latitude, 
-                                                             geo_location.longitude);
+            found_location = GeoInfo.Utils.search_locations (geo_location);
         } catch (IOError e) {
             warning ("obtaining geolocation: %s", e.message);
         }


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