Exemplo n.º 1
0
        public static string GetCityName(string latLng)
        {
            string[]     array          = latLng.Split(',');
            string       str            = array[1] + "," + array[0];
            string       result         = "";
            SiteSettings masterSettings = SettingsManager.GetMasterSettings();
            string       text           = masterSettings.GaoDeAPIKey;

            if (string.IsNullOrEmpty(text))
            {
                text = "53e4f77f686e6a2b5bf53521e178c6e7";
            }
            string url            = "https://restapi.amap.com/v3/geocode/regeo?output=json&radius=3000&location=" + str + "&key=" + text;
            string responseResult = Globals.GetResponseResult(url);
            GaodeGetAddressByLatLngResult gaodeGetAddressByLatLngResult = JsonHelper.ParseFormJson <GaodeGetAddressByLatLngResult>(responseResult);

            if (gaodeGetAddressByLatLngResult.status == 1 && gaodeGetAddressByLatLngResult.info == "OK")
            {
                result = (string.IsNullOrEmpty(gaodeGetAddressByLatLngResult.regeocode.addressComponent.city) ? gaodeGetAddressByLatLngResult.regeocode.addressComponent.province : gaodeGetAddressByLatLngResult.regeocode.addressComponent.city);
            }
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 通过坐标获取地址
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        public static void GetAddressByLatLng(string latLng, ref string address, ref string province, ref string city, ref string district, ref string street)
        {
            string[] latlngarr   = latLng.Split(',');
            string   gaodeLngLat = latlngarr[1] + "," + latlngarr[0];
            string   newLatLng   = Math.Round(decimal.Parse(latlngarr[1]), 4) + "," + Math.Round(decimal.Parse(latlngarr[0]), 4);
            object   objlatlng   = Core.Cache.Get(CacheKeyCollection.LatlngCacheKey(newLatLng));

            if (objlatlng != null)
            {
                GaodeGetAddressByLatLngResult resultobj = (GaodeGetAddressByLatLngResult)objlatlng;
                if (resultobj.status == 1 && resultobj.info == "OK")
                {
                    province = resultobj.regeocode.addressComponent.province;
                    city     = string.IsNullOrEmpty(resultobj.regeocode.addressComponent.city) ? resultobj.regeocode.addressComponent.province : resultobj.regeocode.addressComponent.city;
                    district = resultobj.regeocode.addressComponent.district;
                    street   = resultobj.regeocode.addressComponent.township;
                    if (string.IsNullOrEmpty(resultobj.regeocode.addressComponent.building.name))
                    {
                        address = resultobj.regeocode.addressComponent.neighborhood.name;
                    }
                    else
                    {
                        address = resultobj.regeocode.addressComponent.building.name;
                    }
                    if (string.IsNullOrEmpty(address))
                    {
                        string preAddr = province + resultobj.regeocode.addressComponent.city + district + street;
                        address = resultobj.regeocode.formatted_address.Remove(0, preAddr.Length);
                    }
                }
            }
            else
            {
                string gaoDeAPIKey = "53e4f77f686e6a2b5bf53521e178c6e7";
                string url         = "http://restapi.amap.com/v3/geocode/regeo?output=json&radius=3000&location=" + gaodeLngLat + "&key=" + gaoDeAPIKey;
                string result      = GetResponseResult(url);

                GaodeGetAddressByLatLngResult resultobj = ParseFormJson <GaodeGetAddressByLatLngResult>(result);
                if (resultobj.status == 1 && resultobj.info == "OK")
                {
                    var cacheTimeout = DateTime.Now.AddDays(1);
                    Core.Cache.Insert(CacheKeyCollection.LatlngCacheKey(newLatLng), resultobj, cacheTimeout); //坐标地址信息缓存一天
                    province = resultobj.regeocode.addressComponent.province;
                    city     = string.IsNullOrWhiteSpace(resultobj.regeocode.addressComponent.city) ? resultobj.regeocode.addressComponent.province : resultobj.regeocode.addressComponent.city;
                    district = resultobj.regeocode.addressComponent.district;
                    street   = resultobj.regeocode.addressComponent.township;
                    if (string.IsNullOrWhiteSpace(resultobj.regeocode.addressComponent.building.name))
                    {
                        address = resultobj.regeocode.addressComponent.neighborhood.name;
                    }
                    else
                    {
                        address = resultobj.regeocode.addressComponent.building.name;
                    }
                    if (string.IsNullOrWhiteSpace(address))
                    {
                        string preAddr = province + resultobj.regeocode.addressComponent.city + district + street;
                        address = resultobj.regeocode.formatted_address.Remove(0, preAddr.Length);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public static void GetAddressByLatLng(string latLng, ref string address, ref string province, ref string city, ref string district, ref string street)
        {
            string[] array = latLng.Split(',');
            string   str   = array[1] + "," + array[0];
            string   arg   = Math.Round(decimal.Parse(array[1]), 4) + "," + Math.Round(decimal.Parse(array[0]), 4);
            object   obj   = HiCache.Get($"DataCache-latlngAddress-{arg}");

            if (obj != null)
            {
                GaodeGetAddressByLatLngResult gaodeGetAddressByLatLngResult = (GaodeGetAddressByLatLngResult)obj;
                if (gaodeGetAddressByLatLngResult.status == 1 && gaodeGetAddressByLatLngResult.info == "OK")
                {
                    province = gaodeGetAddressByLatLngResult.regeocode.addressComponent.province;
                    city     = (string.IsNullOrEmpty(gaodeGetAddressByLatLngResult.regeocode.addressComponent.city) ? gaodeGetAddressByLatLngResult.regeocode.addressComponent.province : gaodeGetAddressByLatLngResult.regeocode.addressComponent.city);
                    district = gaodeGetAddressByLatLngResult.regeocode.addressComponent.district;
                    street   = gaodeGetAddressByLatLngResult.regeocode.addressComponent.township;
                    if (string.IsNullOrEmpty(gaodeGetAddressByLatLngResult.regeocode.addressComponent.building.name))
                    {
                        address = gaodeGetAddressByLatLngResult.regeocode.addressComponent.neighborhood.name;
                    }
                    else
                    {
                        address = gaodeGetAddressByLatLngResult.regeocode.addressComponent.building.name;
                    }
                    if (string.IsNullOrEmpty(address))
                    {
                        string text = province + gaodeGetAddressByLatLngResult.regeocode.addressComponent.city + district + street;
                        address = gaodeGetAddressByLatLngResult.regeocode.formatted_address.Remove(0, text.Length);
                    }
                }
            }
            else
            {
                SiteSettings masterSettings = SettingsManager.GetMasterSettings();
                string       text2          = masterSettings.GaoDeAPIKey;
                if (string.IsNullOrEmpty(text2))
                {
                    text2 = "53e4f77f686e6a2b5bf53521e178c6e7";
                }
                string text3 = "https://restapi.amap.com/v3/geocode/regeo?output=json&radius=3000&location=" + str + "&key=" + text2;
                string text4 = "";
                try
                {
                    text4 = Globals.GetResponseResult(text3);
                    GaodeGetAddressByLatLngResult gaodeGetAddressByLatLngResult2 = JsonHelper.ParseFormJson <GaodeGetAddressByLatLngResult>(text4);
                    if (gaodeGetAddressByLatLngResult2.status == 1 && gaodeGetAddressByLatLngResult2.info == "OK")
                    {
                        HiCache.Insert($"DataCache-latlngAddress-{arg}", gaodeGetAddressByLatLngResult2, 86400);
                        province = gaodeGetAddressByLatLngResult2.regeocode.addressComponent.province;
                        city     = (string.IsNullOrEmpty(gaodeGetAddressByLatLngResult2.regeocode.addressComponent.city) ? gaodeGetAddressByLatLngResult2.regeocode.addressComponent.province : gaodeGetAddressByLatLngResult2.regeocode.addressComponent.city);
                        district = gaodeGetAddressByLatLngResult2.regeocode.addressComponent.district;
                        street   = gaodeGetAddressByLatLngResult2.regeocode.addressComponent.township;
                        if (string.IsNullOrEmpty(gaodeGetAddressByLatLngResult2.regeocode.addressComponent.building.name))
                        {
                            address = gaodeGetAddressByLatLngResult2.regeocode.addressComponent.neighborhood.name;
                        }
                        else
                        {
                            address = gaodeGetAddressByLatLngResult2.regeocode.addressComponent.building.name;
                        }
                        if (string.IsNullOrEmpty(address))
                        {
                            string text5 = province + gaodeGetAddressByLatLngResult2.regeocode.addressComponent.city + district + street;
                            address = gaodeGetAddressByLatLngResult2.regeocode.formatted_address.Remove(0, text5.Length);
                        }
                    }
                }
                catch (Exception ex)
                {
                    IDictionary <string, string> dictionary = new Dictionary <string, string>();
                    dictionary.Add("url", text3);
                    dictionary.Add("result", text4);
                    Globals.WriteExceptionLog(ex, dictionary, "GetAddressByLatLngEx");
                }
            }
        }