C#调用百度api,根据经度和纬度获取地理位置信息

        ///


        /// 百度api 根据经纬度获取地理位置
        ///

        /// 经度
        /// 纬度
        /// 具体的地理位置

 

public static string GetLocation( string lng,string lat)
        {

            HttpClient client = new HttpClient();
string url = string.Format("http://api.map.baidu.com/geocoder/v2/?ak=你的AK&callback=renderReverse&location={0},{1}&output=json&pois=1Z", lng,lat);
            string result = client.GetStringAsync(url).Result;
            
var locationResult = (JObject)JsonConvert.DeserializeObject(result.Replace("renderReverse&&renderReverse","").Replace("(","").Replace(")",""));

            if (locationResult == null || locationResult["result"] == null || locationResult["result"]["formatted_address"] == null)
                return string.Empty;

            var address = Convert.ToString(locationResult["result"]["formatted_address"]);
            if (locationResult["result"]["sematic_description"] != null)
                address += " " + Convert.ToString(locationResult["result"]["sematic_description"]);
            return address;
        }

你可能感兴趣的:(日常代码)