php专区

 首页 > php专区 > PHP应用 > 开放平台 > C#微信开发之旅(七):根据经纬度获取地址(百度地

C#微信开发之旅(七):根据经纬度获取地址(百度地

分享到:
【字体:
导读:
          开发过程中遇到这样的需求,根据用户的地理位置不同,显示不同区域的产品。这里用到了微信:获取用户地理位置 的功能,(每隔5秒上报 或 进入回话时上报一次),我们根据微信推...

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

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

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

        #region 根据经纬度 获取地址信息 BaiduApi

        /// 
        /// 根据经纬度  获取 地址信息
        /// 
        /// 经度
        /// 纬度
        /// 
        public static BaiDuGeoCoding GeoCoder(string lat, string lng)
        {
            string url = string.Format(WeiXinConst.Baidu_GeoCoding_ApiUrl, lat, lng);

            var model = HttpClientHelper.GetResponse(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
    {
        /// 
        /// 省份
        /// 
        public string Province { get; set; }
        /// 
        /// 城市名
        /// 
        public string City { get; set; }

        /// 
        /// 区县名
        /// 
        public string District { get; set; }

        /// 
        /// 街道名
        /// 
        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);

分享到:
C#微信开发之旅(八):通过授权code以及op...
要将我们的Web账号和微信账号绑定到一起,必须要用到微信openid的概念,每个账号对于不同公众号都会有一个openid ,可以根据openid与我们的账号关联到一起。 而openid 的获取最主要的: 微信推送给我们的各种消息 网页授权 可能用到的地方: 注册(通过网页授权获取openid,与注册的账户关联起来) 公众号菜单配置...
C#微信开发之旅(六):生成带参数的二维码...
主要用来推广并统计数据,当用户扫描带参数二维码时,微信会推送消息到我们的服务器,此时可以根据参数了解到用户是从哪个渠道关注的。话不多少,先来代码: 1 #region 生成二维码 2 3 /// 4 /// 获取Ticket 5 /// 6 /// 7 private static string Cr...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……