爲了方便你們開發LBS應用,SDK對經常使用計算公式,以及百度和谷歌的地圖接口作了封裝。html
經常使用計算:git
用於計算2個座標點之間的直線距離:Senparc.Weixin.MP.Helpers.Distance(double n1, double e1, double n2, double e2)web
根據距離獲取維度差:Senparc.Weixin.MP.Helpers.GetLatitudeDifference(double km)小程序
根據距離獲取經度差:Senparc.Weixin.MP.Helpers.GetLongitudeDifference(double km)api
百度API類:Senparc.Weixin.MP.Helpers.BaiduMapHelper微信
生成百度靜態地圖URL:BaiduMapHelper.GetBaiduStaticMap(double lng, double lat, int scale, int zoom, IList<BaiduMarkers> markersList, int width = 400, int height = 300)websocket
最後生成的地址以下:微信公衆平臺
http://maps.googleapis.com/maps/api/staticmap?center=&zoom=13&size=640x640&maptype=roadmap&format=jpg&sensor=false&language=zh&&markers=color:red%7Clabel:O%7C31.285774,120.59761&markers=color:blue%7Clabel:T%7C31.289774,120.59791socket
生成的URL能夠直接放到<img>中,或者直接賦值在ResponseMessageNews的Article.PicUrl。ide
對應的GoogleMap API,SDK中作了一致的操做體驗。
GoogleMap API類:Senparc.Weixin.MP.Helpers.GoogleMapHelper
生成百度靜態地圖URL:GoogleMapHelper.GetGoogleStaticMap(int scale, IList<GoogleMapMarkers> markersList, string size = "640x640")
生成的地址以下:
http://maps.googleapis.com/maps/api/staticmap?center=&zoom=&size=640x640&maptype=roadmap&format=jpg&sensor=false&language=zh&&markers=color:red%7Clabel:O%7C31.285774,120.59761&markers=color:blue%7Clabel:T%7C31.289774,120.59791
結合SDk,咱們能夠在用戶發送位置消息過來的時候,使用地圖接口作一些功能,例如咱們在MessageHandler的OnLocationRequest實踐中對消息進行處理:
/// <summary> /// 處理位置請求 /// </summary> /// <param name="requestMessage"></param> /// <returns></returns> public override IResponseMessageBase OnLocationRequest(RequestMessageLocation requestMessage) { var responseMessage = ResponseMessageBase.CreateFromRequestMessage<ResponseMessageNews>(requestMessage); var markersList = new List<GoogleMapMarkers>(); markersList.Add(new GoogleMapMarkers() { X = requestMessage.Location_X, Y = requestMessage.Location_Y, Color = "red", Label = "S", Size = GoogleMapMarkerSize.Default, }); var mapSize = "480x600"; var mapUrl = GoogleMapHelper.GetGoogleStaticMap(19 /*requestMessage.Scale*//*微信和GoogleMap的Scale不一致,這裏建議使用固定值*/, markersList, mapSize); responseMessage.Articles.Add(new Article() { Description = string.Format("您剛纔發送了地理位置信息。Location_X:{0},Location_Y:{1},Scale:{2},標籤:{3}", requestMessage.Location_X, requestMessage.Location_Y, requestMessage.Scale, requestMessage.Label), PicUrl = mapUrl, Title = "定位地點周邊地圖", Url = mapUrl }); responseMessage.Articles.Add(new Article() { Title = "微信公衆平臺SDK 官網連接", Description = "Senparc.Weixin.MK SDK地址", PicUrl = "http://weixin.senparc.com/images/logo.jpg", Url = "http://weixin.senparc.com" });
return responseMessage;
}
實際的開發過程當中,除了輸出位置的信息,咱們還能夠根據用戶的當前位置,檢索就近的點,在Articles中輸出,並計算出距離。
地址:http://www.cnblogs.com/szw/archive/2013/05/14/weixin-course-index.html