예제 #1
0
 public LocationInfo ToLocation()
 {
   var loc = new LocationInfo { Latitude = Latitude, Longitude = Longitude };
   if (AddressInfo.ContainsKey(KEY_CITY))
     loc.City = AddressInfo[KEY_CITY];
   if (AddressInfo.ContainsKey(KEY_STATE))
     loc.State = AddressInfo[KEY_STATE];
   if (AddressInfo.ContainsKey(KEY_COUNTRY))
     loc.Country = AddressInfo[KEY_COUNTRY];
   return loc;
 }
예제 #2
0
 public bool TryLookup(double latitude, double longitude, out LocationInfo locationInfo)
 {
   var downloader = new Downloader { EnableCompression = true };
   ReverseGeoCodeResult result = downloader.Download<ReverseGeoCodeResult>(BuildUrl(latitude, longitude));
   if (result == null)
   {
     locationInfo = null;
     return false;
   }
   locationInfo = result.ToLocation();
   return !string.IsNullOrWhiteSpace(locationInfo.Country) || !string.IsNullOrWhiteSpace(locationInfo.State) || !string.IsNullOrWhiteSpace(locationInfo.City);
 }
예제 #3
0
 public LocationInfo ToLocation ()
 {
   LocationInfo result = new LocationInfo();
   var city = GetAddressComponent("locality");
   if (city != null)
     result.City = city.LongName;
   var state = GetAddressComponent("administrative_area_level_1");
   if (state != null)
     result.State = state.LongName;
   var country = GetAddressComponent("country");
   if (country != null)
     result.Country = country.LongName;
   return result;
 }
예제 #4
0
 public bool TryLookup(double latitude, double longitude, out LocationInfo locationInfo)
 {
   var downloader = new Downloader { EnableCompression = true };
   downloader.Headers["Accept"] = "application/json";
   // Google enables compressed output only, if a valid User-Agent is sent!
   downloader.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0";
   GoogleResults results = downloader.Download<GoogleResults>(BuildUrl(latitude, longitude));
   if (results == null || results.Results == null || results.Results.Count == 0)
   {
     locationInfo = null;
     return false;
   }
   locationInfo = results.Results[0].ToLocation();
   locationInfo.Latitude = latitude;
   locationInfo.Longitude = longitude;
   return true;
 }
예제 #5
0
        public LocationInfo ToLocation()
        {
            LocationInfo result = new LocationInfo();
            var          city   = GetAddressComponent("locality");

            if (city != null)
            {
                result.City = city.LongName;
            }
            var state = GetAddressComponent("administrative_area_level_1");

            if (state != null)
            {
                result.State = state.LongName;
            }
            var country = GetAddressComponent("country");

            if (country != null)
            {
                result.Country = country.LongName;
            }
            return(result);
        }
예제 #6
0
    public bool TryLookup(double latitude, double longitude, out LocationInfo locationInfo)
    {
      try
      {
        if (GetFromCache(latitude, longitude, out locationInfo))
          return true;

        if (NetworkConnectionTracker.IsNetworkConnected)
          foreach (IGeolocationLookup lookup in GetOnlineServices())
          {
            if (lookup.TryLookup(latitude, longitude, out locationInfo))
            {
              _locationCache.Add(locationInfo);
              return true;
            }
          }
      }
      catch (Exception ex)
      {
        ServiceRegistration.Get<ILogger>().Error("Error while executing reverse geocoding.", ex);
      }
      locationInfo = null;
      return false;
    }
예제 #7
0
 private bool GetFromCache(double latitude, double longitude, out LocationInfo locationInfo)
 {
   LocationInfo newLoc = new LocationInfo { Latitude = latitude, Longitude = longitude };
   // We cannot compare for exact lat/long values, but we consider points within the CACHE_MAX_DISTANCE_KM 
   // to belong to same location.
   foreach (LocationInfo info in _locationCache.Where(info => CalculateDistance(info, newLoc) <= CACHE_MAX_DISTANCE_KM))
   {
     locationInfo = info;
     return true;
   }
   locationInfo = null;
   return false;
 }
예제 #8
0
    public static double CalculateDistance(LocationInfo location1, LocationInfo location2)
    {
      const double EARTH_RADIUS_KM = 6376.5;
      double lat1InRad = DegreesToRadians(location1.Latitude);
      double long1InRad = DegreesToRadians(location1.Longitude);
      double lat2InRad = DegreesToRadians(location2.Latitude);
      double long2InRad = DegreesToRadians(location2.Longitude);

      double dLongitude = long2InRad - long1InRad;
      double dLatitude = lat2InRad - lat1InRad;
      double a = Math.Pow(Math.Sin(dLatitude / 2), 2) + Math.Cos(lat1InRad) * Math.Cos(lat2InRad) * Math.Pow(Math.Sin(dLongitude / 2), 2);
      double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
      return EARTH_RADIUS_KM * c;
    }