/// <summary> /// Gets the best address for coordinates. /// </summary> /// <param name="coordinates">The coordinates.</param> /// <returns></returns> protected Address GetBestAddressForCoordinates(Coordinates coordinates) { Address address = GeoLookupCache.GetOrAdd(coordinates.ToString(), (key) => { var client = new RestSharp.RestClient("https://reverse.geocoder.api.here.com/6.2"); var req = new RestSharp.RestRequest("reversegeocode.json"); req.AddParameter("prox", coordinates.ToString()); req.AddParameter("mode", "retrieveAddresses"); req.AddParameter("maxresults", 1); req.AddParameter("app_id", Properties.Settings.Default.HereAppId); req.AddParameter("app_code", Properties.Settings.Default.HereAppCode); var resp = client.Execute <HereRestApi.ApiResponse <HereRestApi.LocationResult> >(req); lock ( GeoLookupCache ) { GeoLookupCount += 1; } if (!resp.Data.Response.View.Any() || !resp.Data.Response.View.First().Result.Any()) { return(new Address { Street1 = DataFaker.Address.StreetAddress(), City = DataFaker.Address.City(), State = DataFaker.Address.State(), County = DataFaker.Address.County(), PostalCode = DataFaker.Address.ZipCode(), Country = "US" }); } else { var location = resp.Data.Response.View.First().Result.First().Location; return(new Address { Street1 = $"{ location.Address.HouseNumber } { location.Address.Street }", City = location.Address.City, State = location.Address.State, County = location.Address.County, PostalCode = location.Address.PostalCode, Country = location.Address.Country.Substring(0, 2) }); } }); // // Save the cache every 100 lookups. That way, if there is a crash, we don't lose everything. // lock ( GeoLookupCache ) { if (GeoLookupCount > 100) { Support.SaveGeocodeCache(GeoLookupCache.ToDictionary(kvp => kvp.Key, kvp => kvp.Value)); GeoLookupCount = 0; } } return(address); }
/// <summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// </summary> public void Dispose() { Support.SaveGeocodeCache(GeoLookupCache.ToDictionary(kvp => kvp.Key, kvp => kvp.Value)); }