zoukankan      html  css  js  c++  java
  • C#微信开发之旅(七):根据经纬度获取地址(百度地图Api)

    开发过程中遇到这样的需求,根据用户的地理位置不同,显示不同区域的产品。

    这里用到了微信:获取用户地理位置 的功能,(每隔5秒上报 或 进入回话时上报一次),我们根据微信推送过来的经纬度,来转换成实际地址,这里用到的是百度地图Api(要用的话先申请百度ak)。

    PS:微信的这个功能很不稳定,靠它不靠谱,经常不推送。。。(后来加了手动定位,百度地图Web定位组件 还不错,不是广告!0.0)

            #region 根据经纬度 获取地址信息 BaiduApi
    
            /// <summary>
            /// 根据经纬度  获取 地址信息
            /// </summary>
            /// <param name="lat">经度</param>
            /// <param name="lng">纬度</param>
            /// <returns></returns>
            public static BaiDuGeoCoding GeoCoder(string lat, string lng)
            {
                string url = string.Format(WeiXinConst.Baidu_GeoCoding_ApiUrl, lat, lng);
    
                var model = HttpClientHelper.GetResponse<BaiDuGeoCoding>(url);
    
                return model;
            }
    
            #endregion

    BaiduGeoCoding是针对Api相应结果封装的对象:

    public class BaiDuGeoCoding
        {
            public int Status { get; set; }
            public Result Result { get; set; }
        }
    
        public class Result
        {
            public Location Location { get; set; }
    
            public string Formatted_Address { get; set; }
    
            public string Business { get; set; }
    
            public AddressComponent AddressComponent { get; set; }
    
            public string CityCode { get; set; }
        }
    
        public class AddressComponent
        {
            /// <summary>
            /// 省份
            /// </summary>
            public string Province { get; set; }
            /// <summary>
            /// 城市名
            /// </summary>
            public string City { get; set; }
    
            /// <summary>
            /// 区县名
            /// </summary>
            public string District { get; set; }
    
            /// <summary>
            /// 街道名
            /// </summary>
            public string Street { get; set; }
    
            public string Street_number { get; set; }
    
        }
    
        public class Location
        {
            public string Lng { get; set; }
            public string Lat { get; set; }
        }

    调用:

                //需配置 WeiXineConst的BaiduAk
                string lat = "31.1430"; //经度
                string lng = "121.2943";// 纬度
                var model = WeiXinHelper.GeoCoder(lat, lng);
  • 相关阅读:
    EXCEL文本转数值方法我找的好苦啊(转) Kevin
    C# 自定义控件和自定义事件 Kevin
    MVC3 示例项目中 Authentication 验证密码错误。 Kevin
    如何选定文件或文件夹
    软件工程师的必修课:PKM
    “个人知识管理”的定义和包含的内容
    如何评价一个专业性的工具软件
    教你从“搜索”的角度来选取个人知识管理软件
    国内领先的PKM(个人知识库管理)工具
    如果界面还不行就跳闽江
  • 原文地址:https://www.cnblogs.com/hetring/p/4054595.html
Copyright © 2011-2022 走看看