private GeoCodedLocation BuildLocation(GeoCodeResults results, string geocodeText) { GeoCodedLocation location = null; var result = results.results.FirstOrDefault(); location = new GeoCodedLocation { ID = result.id, CountryCode = result.address.countryCode, FormattedLocation = result.address.freeformAddress, Latitude = (decimal)result.position.lat, Longitude = (decimal)result.position.lon, GeocodeTerm = geocodeText, PostalCode = result.address.postalCode, StateCode = result.address.countrySubdivision, City = result.address.municipality }; if (!string.IsNullOrEmpty(result.address.streetNumber) && !string.IsNullOrEmpty(result.address.streetName)) { location.Address = string.Format("{0} {1}", result.address.streetNumber, result.address.streetName); } location.Precision = MapPrecision(result).ToString(); return(location); }
public async Task <GeoCodedLocation> FuzzyGeoCode(string text) { GeoCodedLocation geoCodedLocation = null; try { text = text.ToLower().Trim(); IAmazonDynamoDB clientDynamoDb = new AmazonDynamoDBClient(AWS_KEY, AWS_SECRET, RegionEndpoint.USWest2); DynamoDBContext context = new DynamoDBContext(clientDynamoDb); geoCodedLocation = await context.LoadAsync <GeoCodedLocation>(text); if (geoCodedLocation != null) { geoCodedLocation.Source = "Cache"; return(geoCodedLocation); } var client = new RestClient(BASE_URL); var request = new RestRequest(string.Format("/search/fuzzy/json?query={0}, USA&api-version=1.0&subscription-key={1}", text, AZURE_MAP_KEY), Method.GET); request.AddHeader("Accept", "application/json, text/json, text/x-json"); var typedRestData = client.Execute <GeoCodeResults>(request); if (typedRestData.Data != null && typedRestData.StatusCode == System.Net.HttpStatusCode.OK && typedRestData.Data.results.Any()) { geoCodedLocation = BuildLocation(typedRestData.Data, text); geoCodedLocation.CacheDate = DateTime.Today; await context.SaveAsync(geoCodedLocation); geoCodedLocation.Source = "Geocoder"; return(geoCodedLocation); } } catch (Exception ex) { } return(null); }