コード例 #1
0
ファイル: Cache.cs プロジェクト: MartyZhou/mytest
        public static async Task <CityLocation> GetCityName(char latRef, double lat, char lonRef, double lon)
        {
            var cityLocation = new CityLocation();

            AddressDetails addressDetailsInCache;
            AddressDetails parentAddressDetailsInCache;

            if (TryGetAddressComponentFromCache(latRef, lat, lonRef, lon, out addressDetailsInCache, out parentAddressDetailsInCache))
            {
                cityLocation = ParseCityNameAndInsertCache(addressDetailsInCache, new AddressResult());
            }
            else
            {
                var address = await LoadRawAddressFromServerAsync(latRef, lat, lonRef, lon).ConfigureAwait(false);

                if (address.IsValid)
                {
                    cityLocation = ParseCityNameAndInsertCache(address.Results[0], address);
                }
                else if (!string.IsNullOrWhiteSpace(parentAddressDetailsInCache.PlaceId))
                {
                    cityLocation = ParseCityNameAndInsertCache(parentAddressDetailsInCache, new AddressResult());
                }
            }

            return(cityLocation);
        }
コード例 #2
0
ファイル: Cache.cs プロジェクト: MartyZhou/mytest
        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);
        }