Пример #1
0
        private static void LinkToParent(AddressResult raw, int index, Node node)
        {
            var parentPlaceId = raw.Results[index].PlaceId;

            if (!totalAddressCache.ContainsKey(parentPlaceId))
            {
                var parent = new Node(raw.Results[index]);
                totalAddressCache.Add(parent.PlaceId, parent);
            }

            var parentNode = totalAddressCache[parentPlaceId];

            node.LinkParent(parentNode);

            if (node.Bounds.Norteast.Lat == 0 &&
                node.Bounds.Norteast.Lng == 0 &&
                node.Bounds.Southwest.Lat == 0 &&
                node.Bounds.Southwest.Lng == 0 &&
                !leafAddressCache.ContainsKey(parentNode.PlaceId))
            {
                leafAddressCache.Add(parentNode.PlaceId, parentNode);
            }

            index++;
            if (parentNode.Parent == null && index < raw.Results.Length)
            {
                LinkToParent(raw, index, parentNode);
            }
        }
Пример #2
0
        private static void ParseRawAddress(AddressResult raw)
        {
            var firstPlaceId = raw.Results[0].PlaceId;

            if (!leafAddressCache.ContainsKey(firstPlaceId))
            {
                var firstNode = new Node(raw.Results[0]);
                if (firstNode.LocationType != LocationType.NONE)
                {
                    leafAddressCache.Add(firstNode.PlaceId, firstNode);
                }
            }

            Node leaf;

            if (leafAddressCache.TryGetValue(firstPlaceId, out leaf))
            {
                if (leaf.Parent == null && raw.Results.Length > 1)
                {
                    LinkToParent(raw, 1, leaf);
                }
            }
        }
Пример #3
0
        private static async Task <AddressResult> LoadRawAddressFromServerAsync(char latRef, double absLat, char lonRef, double absLon)
        {
            var result = new AddressResult();

            try
            {
                using (var httpClient = new HttpClient())
                {
                    var lat = latRef == 'N' ? absLat : -absLat;
                    var lon = lonRef == 'E' ? absLon : -absLon;
                    var url = string.Format("https://maps.googleapis.com/maps/api/geocode/json?latlng={0},{1}&key={2}", lat, lon, API_KEY);

                    var response = await httpClient.GetStringAsync(url).ConfigureAwait(false);

                    if (!string.IsNullOrWhiteSpace(response))
                    {
                        Console.WriteLine(string.Format("[LocationCache]################ LoadRawAddressFromServerAsync successfully and save to {0}", GenerateFileName(latRef, absLat, lonRef, absLon)));

                        result         = JsonConvert.DeserializeObject <AddressResult>(response);
                        result.IsValid = true;

                        File.WriteAllText(GenerateFileName(latRef, absLat, lonRef, absLon), JsonConvert.SerializeObject(result));
                    }
                    else
                    {
                        Console.WriteLine(string.Format("[LocationCache]################ Failed to LoadRawAddressFromServerAsync: {0}", GenerateFileName(latRef, absLat, lonRef, absLon)));
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(string.Format("Failed to call google map API: {0}", e.Message));
                throw;
            }

            return(result);
        }
Пример #4
0
        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));
                }
            }
        }
Пример #5
0
        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);
        }