private static bool TryGetAddressComponentFromCache(char latRef, double absLat, char lonRef, double absLon, out AddressDetails match, out AddressDetails parentMatch) { var result = false; var parentResult = false; match = new AddressDetails(); parentMatch = new AddressDetails(); var lat = latRef == 'N' ? absLat : -absLat; var lon = lonRef == 'E' ? absLon : -absLon; result = TryGetAddressComponentFromCache(latRef, lat, lonRef, lon, cityLevelAddressComponentCache, out match); if (!result) { parentResult = TryGetAddressComponentFromCache(latRef, lat, lonRef, lon, areaLevel2AddressComponentCache, out parentMatch); if (!parentResult) { parentResult = TryGetAddressComponentFromCache(latRef, lat, lonRef, lon, areaLevel1AddressComponentCache, out parentMatch); } if (!parentResult) { parentResult = TryGetAddressComponentFromCache(latRef, lat, lonRef, lon, countryLevelAddressComponentCache, out parentMatch); } } //Console.WriteLine("test ################ TryGetAddressComponentFromCache end " + result); return(result); }
public Node(AddressDetails address) { this.PlaceId = address.PlaceId; this.FormattedAddress = address.FormattedAddress; this.Bounds = address.Geometry.Bounds; LocationType locationType; Enum.TryParse <LocationType>(address.Geometry.LocationType, out locationType); this.LocationType = locationType; this.Location = address.Geometry.Location; this.Components = new AddressComponent[address.AddressComponents.Length]; for (int i = 0; i < address.AddressComponents.Length; i++) { this.Components.SetValue(new AddressComponent(address.AddressComponents[i]), i); } }
private static bool TryGetAddressComponentFromCache(char latRef, double lat, char lonRef, double lon, ConcurrentDictionary <string, AddressDetails> cache, out AddressDetails match) { var result = false; match = new AddressDetails(); foreach (var addressComponent in cache) { if (addressComponent.Value.Geometry.LocationType == LOCATION_TYPE_GEOMETRIC_CENTER) { var location = addressComponent.Value.Geometry.Location; var latTest = lat <= location.Lat + config.CenterTolerance && lat >= location.Lat - config.CenterTolerance; var lonTest = lon <= location.Lng + config.CenterTolerance && lon >= location.Lng - config.CenterTolerance; if (latTest && lonTest) { Console.WriteLine(string.Format("[LocationCache]################ found match by GEOMETRIC_CENTER KEY: {0}, address: {1}", addressComponent.Key, JsonConvert.SerializeObject(addressComponent.Value.FormattedAddress))); match = addressComponent.Value; result = true; } } else { var bounds = addressComponent.Value.Geometry.Bounds; var latTest = lat <= bounds.Norteast.Lat && lat >= bounds.Southwest.Lat; var lonTest = lon <= bounds.Norteast.Lng && lon >= bounds.Southwest.Lng; if (latTest && lonTest) { Console.WriteLine(string.Format("[LocationCache]################ found match KEY: {0}, address: {1}", addressComponent.Key, JsonConvert.SerializeObject(addressComponent.Value.FormattedAddress))); match = addressComponent.Value; result = true; } } } return(result); }
private static void InsertAddressComponentToCache(AddressResult addressResult, string areaLevel) { AddressDetails countryArea = new AddressDetails(); AddressDetails areaLevel1 = new AddressDetails(); AddressDetails areaLevel2 = new AddressDetails(); AddressDetails cityArea = new AddressDetails(); foreach (var addressComponent in addressResult.Results) { foreach (var addressType in addressComponent.Types) { if (addressType == areaLevel) { if (!cityLevelAddressComponentCache.ContainsKey(addressComponent.PlaceId)) { cityLevelAddressComponentCache.TryAdd(addressComponent.PlaceId, addressComponent); //Console.WriteLine(string.Format("[LocationCache]################ InsertAddressComponentToCache. AddressLevel: {0}, PlaceId: {1}, Address: {2}", areaLevel, addressComponent.PlaceId, addressComponent.FormattedAddress)); } break; } else if (addressType == CITY_LEVEL) { cityArea = addressComponent; } else if (addressType == COUNTRY_LEVEL) { countryArea = addressComponent; } else if (addressType == AREA_LEVEL1) { areaLevel1 = addressComponent; } else if (addressType == AREA_LEVEL2) { areaLevel2 = addressComponent; } } } if (!string.IsNullOrWhiteSpace(countryArea.PlaceId)) { if (!countryLevelAddressComponentCache.ContainsKey(countryArea.PlaceId)) { countryLevelAddressComponentCache.TryAdd(countryArea.PlaceId, countryArea); //Console.WriteLine(string.Format("[LocationCache]################ InsertAddressComponentToCache. AddressLevel: {0}, PlaceId: {1}, Address: {2}", COUNTRY_LEVEL, countryArea.PlaceId, addressResult.Results[0].FormattedAddress)); } } if (!string.IsNullOrWhiteSpace(areaLevel1.PlaceId)) { if (!areaLevel1AddressComponentCache.ContainsKey(areaLevel1.PlaceId)) { areaLevel1AddressComponentCache.TryAdd(areaLevel1.PlaceId, areaLevel1); // Console.WriteLine(string.Format("[LocationCache]################ InsertAddressComponentToCache. AddressLevel: {0}, PlaceId: {1}, Address: {2}", AREA_LEVEL1, areaLevel1.PlaceId, addressResult.Results[0].FormattedAddress)); } } if (!string.IsNullOrWhiteSpace(areaLevel2.PlaceId)) { if (!areaLevel2AddressComponentCache.ContainsKey(areaLevel2.PlaceId)) { areaLevel2AddressComponentCache.TryAdd(areaLevel2.PlaceId, areaLevel2); //Console.WriteLine(string.Format("[LocationCache]################ InsertAddressComponentToCache. AddressLevel: {0}, PlaceId: {1}, Address: {2}", AREA_LEVEL2, areaLevel2.PlaceId, addressResult.Results[0].FormattedAddress)); } } if (!string.IsNullOrWhiteSpace(cityArea.PlaceId)) { if (!cityLevelAddressComponentCache.ContainsKey(cityArea.PlaceId)) { cityLevelAddressComponentCache.TryAdd(cityArea.PlaceId, cityArea); //Console.WriteLine(string.Format("[LocationCache]################ InsertAddressComponentToCache. AddressLevel: {0}, PlaceId: {1}, Address: {2}", CITY_LEVEL, cityArea.PlaceId, addressResult.Results[0].FormattedAddress)); } } }
private static CityLocation ParseCityNameAndInsertCache(AddressDetails addressDetails, AddressResult addressResult) { var cityLocation = new CityLocation(); var areaLevel = string.Empty; for (var i = addressDetails.AddressComponents.Length - 1; i > -1; i--) { var addressComponent = addressDetails.AddressComponents[i]; foreach (var addressType in addressComponent.Types) { if (addressType == CITY_LEVEL) { cityLocation.City = addressComponent.LongName; } else if (addressType == AREA_LEVEL2) { cityLocation.AreaLevel2 = addressComponent.LongName; } else if (addressType == AREA_LEVEL1) { cityLocation.AreaLevel1 = addressComponent.LongName; } else if (addressType == COUNTRY_LEVEL) { cityLocation.Country = addressComponent.LongName; } } } if (addressResult.IsValid) { if (string.IsNullOrWhiteSpace(cityLocation.City)) { if (string.IsNullOrWhiteSpace(cityLocation.AreaLevel2)) { if (string.IsNullOrWhiteSpace(cityLocation.AreaLevel1)) { if (!string.IsNullOrWhiteSpace(cityLocation.Country)) { areaLevel = COUNTRY_LEVEL; } } else { areaLevel = AREA_LEVEL1; } } else { areaLevel = AREA_LEVEL2; } } else { areaLevel = CITY_LEVEL; } if (!string.IsNullOrWhiteSpace(areaLevel)) { //Console.WriteLine(string.Format("[LocationCache]################ ParseCityNameAndInsertCache. AddressLevel: {0}", areaLevel)); InsertAddressComponentToCache(addressResult, areaLevel); } else { //Console.WriteLine("[LocationCache]################ ParseCityNameAndInsertCache. AddressLevel is empty"); } if (addressDetails.Geometry.LocationType == LOCATION_TYPE_GEOMETRIC_CENTER) { areaLevel = addressDetails.Types[0]; InsertAddressComponentToCache(addressResult, areaLevel); } } return(cityLocation); }