為了方便大家開發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實踐中對消息進行處理:
%20///%20<summary>%20%20%20%20%20%20%20%20///%20處理位置請求%20%20%20%20%20%20%20%20///%20</summary>%20%20%20%20%20%20%20%20///%20<param%20name="requestMessage"></param>%20%20%20%20%20%20%20%20///%20<returns></returns>%20%20%20%20%20%20%20%20public%20override%20IResponseMessageBase%20OnLocationRequest(RequestMessageLocation%20requestMessage)%20%20%20%20%20%20%20%20{%20%20%20%20%20%20%20%20%20%20%20%20var%20responseMessage%20=%20ResponseMessageBase.CreateFromRequestMessage<ResponseMessageNews>(requestMessage);%20%20%20%20%20%20%20%20%20%20%20%20var%20markersList%20=%20new%20List<GoogleMapMarkers>();%20%20%20%20%20%20%20%20%20%20%20%20markersList.Add(new%20GoogleMapMarkers()%20%20%20%20%20%20%20%20%20%20%20%20{%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20X%20=%20requestMessage.Location_X,%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Y%20=%20requestMessage.Location_Y,%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Color%20=%20"red",%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Label%20=%20"S",%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Size%20=%20GoogleMapMarkerSize.Default,%20%20%20%20%20%20%20%20%20%20%20%20});%20%20%20%20%20%20%20%20%20%20%20%20var%20mapSize%20=%20"480x600";%20%20%20%20%20%20%20%20%20%20%20%20var%20mapUrl%20=%20GoogleMapHelper.GetGoogleStaticMap(19%20/*requestMessage.Scale*//*微信和GoogleMap的Scale不一致,這里建議使用固定值*/,%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20markersList,%20mapSize);%20%20%20%20%20%20%20%20%20%20%20%20responseMessage.Articles.Add(new%20Article()%20%20%20%20%20%20%20%20%20%20%20%20{%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Descrreturn responseMessage; }實際的開發過程中,除了輸出位置的信息,我們還可以根據用戶的當前位置,檢索就近的點,在Articles中輸出,并計算出距離。
系列教程索引:http://www.49028c.com/szw/archive/2013/05/14/weixin-course-index.html
新聞熱點
疑難解答