首頁  >  文章  >  微信小程式  >  C#微信開發:根據經緯度取得地址

C#微信開發:根據經緯度取得地址

高洛峰
高洛峰原創
2017-03-04 15:00:331864瀏覽

開發過程中遇到這樣的需求,根據使用者的地理位置不同,顯示不同區域的產品。

這裡用到了微信:取得使用者地理位置的功能,(每隔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);

更多C#微信開發:根據經緯度取得地址相關文章請關注PHP中文網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn