开发过程中遇到这样的需求,根据用户的地理位置不同,显示不同区域的产品。
这里用到了微信:获取用户地理位置 的功能,(每隔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#微信开发:根据经纬度获取地址。