/// <summary> /// 通过坐标获取地址 /// </summary> /// <param name="address"></param> /// <returns></returns> public static QQGetAddressByLatLngResult 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); var objlatlng = Core.Cache.Get <QQGetAddressByLatLngResult>(CacheKeyCollection.LatlngCacheKey(newLatLng)); if (objlatlng != null) { QQGetAddressByLatLngResult resultobj = objlatlng; if (resultobj.status == 0) { province = resultobj.result.address_component.province; city = resultobj.result.address_component.city; district = resultobj.result.address_component.district; if (null != resultobj.result.address_reference && null != resultobj.result.address_reference.town && !string.IsNullOrEmpty(resultobj.result.address_reference.town.title)) { street = resultobj.result.address_reference.town.title; } else { street = resultobj.result.address_component.street; } address = resultobj.result.address; } return(resultobj); } else { string gaoDeAPIKey = "SYJBZ-DSLR3-IWX3Q-3XNTM-ELURH-23FTP"; string url = string.Format("http://apis.map.qq.com/ws/geocoder/v1/?location={0}&key={1}&get_poi=1", latLng, gaoDeAPIKey); string result = GetResponseResult(url); QQGetAddressByLatLngResult resultobj = result.FromJSON <QQGetAddressByLatLngResult>(new QQGetAddressByLatLngResult { }); if (resultobj.status == 0) { var cacheTimeout = DateTime.Now.AddDays(1); Core.Cache.Insert(CacheKeyCollection.LatlngCacheKey(newLatLng), resultobj, cacheTimeout); //坐标地址信息缓存一天 province = resultobj.result.address_component.province; city = resultobj.result.address_component.city; district = resultobj.result.address_component.district; if (null != resultobj.result.address_reference && null != resultobj.result.address_reference.town && !string.IsNullOrEmpty(resultobj.result.address_reference.town.title)) { street = resultobj.result.address_reference.town.title; } else { street = resultobj.result.address_component.street; } address = resultobj.result.address; } return(resultobj); } }
/// <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); } } } }
/// <summary> /// 通过坐标获取地址 /// </summary> /// <param name="address"></param> /// <returns></returns> public static GaodeGetAddressByLatLngResult GetAddressByLatLng(string latLng, ref string address, ref string province, ref string city, ref string district, ref string street) { GaodeGetAddressByLatLngResult result; string str3; string[] strArray = latLng.Split(new char[] { ',' }); string str = strArray[1] + "," + strArray[0]; string fromLatLng = Math.Round(decimal.Parse(strArray[1]), 4) + "," + Math.Round(decimal.Parse(strArray[0]), 4); object obj2 = Cache.Get(CacheKeyCollection.LatlngCacheKey(fromLatLng)); if (obj2 != null) { result = (GaodeGetAddressByLatLngResult)obj2; if ((result.status == 1) && (result.info == "OK")) { province = result.regeocode.addressComponent.province; city = string.IsNullOrEmpty(result.regeocode.addressComponent.city) ? result.regeocode.addressComponent.province : result.regeocode.addressComponent.city; district = result.regeocode.addressComponent.district; street = result.regeocode.addressComponent.township; if (string.IsNullOrEmpty(result.regeocode.addressComponent.building.name)) { address = result.regeocode.addressComponent.neighborhood.name; } else { address = result.regeocode.addressComponent.building.name; } if (string.IsNullOrEmpty(address)) { str3 = province + result.regeocode.addressComponent.city + district + street; address = result.regeocode.formatted_address.Remove(0, str3.Length); } } return(result); } string str4 = "53e4f77f686e6a2b5bf53521e178c6e7"; result = ParseFormJson <GaodeGetAddressByLatLngResult>(GetResponseResult("http://restapi.amap.com/v3/geocode/regeo?output=json&radius=3000&location=" + str + "&key=" + str4)); if ((result.status == 1) && (result.info == "OK")) { DateTime cacheTime = DateTime.Now.AddDays(1.0); Cache.Insert <GaodeGetAddressByLatLngResult>(CacheKeyCollection.LatlngCacheKey(fromLatLng), result, cacheTime); province = result.regeocode.addressComponent.province; city = string.IsNullOrWhiteSpace(result.regeocode.addressComponent.city) ? result.regeocode.addressComponent.province : result.regeocode.addressComponent.city; district = result.regeocode.addressComponent.district; street = result.regeocode.addressComponent.township; if (string.IsNullOrWhiteSpace(result.regeocode.addressComponent.building.name)) { address = result.regeocode.addressComponent.neighborhood.name; } else { address = result.regeocode.addressComponent.building.name; } if (string.IsNullOrWhiteSpace(address)) { str3 = province + result.regeocode.addressComponent.city + district + street; address = result.regeocode.formatted_address.Remove(0, str3.Length); } } return(result); }