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

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

        /// <param name="lng">经度</param>
        /// <param name="lat">纬度</param>
        /// <returns>具体的地理位置</returns>

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;
        }

发布了17 篇原创文章 · 获赞 4 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/ruo40018293/article/details/82985708
今日推荐