為了方便大家開發LBS應用,SDK對常用計算公式,以及百度和谷歌的地圖接口做了封裝。
常用計算:
用於計算2個坐標點之間的直線距離:Senparc.Weixin.MP.Helpers.Distance(double n1, double e1, double n2, double e2)
根據距離獲取維度差:Senparc.Weixin.MP.Helpers.GetLatitudeDifference(double km)
根據距離獲取經度差:Senparc.Weixin.MP.Helpers.GetLongitudeDifference(double km)
百度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)
最後生成的地址如下:
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.59791
生成的URL可以直接放到<img>中,或者直接賦值在ResponseMessageNews的Article.PicUrl。
對應的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