(C#)基于百度api实现经纬度查询地址

需要自己去百度开发者平台申请key

http://lbsyun.baidu.com/index.php?title=%E9%A6%96%E9%A1%B5

        /// <summary>
        /// 根据经纬度获取地理位置
        /// </summary>
        /// <param name="lat">纬度</param>
        /// <param name="lng">经度</param>
        /// <returns>具体的地埋位置</returns>
        public static VmAddress GetLocation(double lat, double lng)
        {

            //百度api
            string url = @"http://api.map.baidu.com/geocoder/v2/?location={0}&output=json&ak=你的key";

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

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

            var privince = Convert.ToString(locationResult["result"]["addressComponent"]["province"]);

            var city = Convert.ToString(locationResult["result"]["addressComponent"]["city"]);
            //到这里省市县都可以拿得到了,由于我只需要省市,其它的就没有要了,可以从locationResult中取出来
            VmAddress vmAddress = new VmAddress()
            {
                Privince = privince,
                City = city,
                Address = address
            };


            return vmAddress;
        }

 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值