隐藏

百度地图api根据详细地址反查坐标

发布:2021/4/9 11:24:45作者:管理员 来源:本站 浏览次数:1210

 var address = "徐州丰沙路与师顺路交叉口";
            var getUrl = $"http://api.map.baidu.com/geocoder/v2/?output=json&ak=1XjLLEhZhQNUzd93EjU5nOGQ&address={address}";
            var str= HttpRequestHelper.HttpGetRequest(getUrl);
            var locationResult = (JObject)JsonConvert.DeserializeObject(str);
            //JArray res = locationResult["result"]["location"].Value<JArray>("location");
            //JObject obj = JObject.Parse(res[0].ToString());//只获取数据结构中第一个userinfo里的数据信息
            var lng = locationResult["result"]["location"]["lng"].ToString();//access_token
            var lat= locationResult["result"]["location"]["lat"].ToString();//access_token
            var s= GetLocation(lat, lng);
            Console.WriteLine(s);

            Console.WriteLine(str);

//百度api
        private static string url = @"http://api.map.baidu.com/geocoder/v2/?location={0}&output=json&ak=WEc8RlPXzSifaq9RHxE1WW7lRKgbid6Y";
        /// <summary>
        /// 根据经纬度获取地理位置
        /// </summary>
        /// <param name="lat">纬度</param>
        /// <param name="lng">经度</param>
        /// <returns>具体的地埋位置</returns>
        public static JObject GetLocation(string lat, string lng)
        {
            HttpClient client = new HttpClient();
            string location = string.Format("{0},{1}", lat, lng);
            string bdUrl = string.Format(url, location);
            string result = client.GetStringAsync(bdUrl).Result;
            var locationResult = (JObject)JsonConvert.DeserializeObject(result);
            /*
            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 locationResult;
        }