Senparc.Weixin.MP SDK 微信公眾平台開發教程(十三):地圖相關接口說明


  為了方便大家開發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

  1. Senparc.Weixin.MP SDK 微信公眾平台開發教程(一):微信公眾平台注冊
  2. Senparc.Weixin.MP SDK 微信公眾平台開發教程(二):成為開發者
  3. Senparc.Weixin.MP SDK 微信公眾平台開發教程(三):微信公眾平台開發驗證
  4. Senparc.Weixin.MP SDK 微信公眾平台開發教程(四):Hello World
  5. Senparc.Weixin.MP SDK 微信公眾平台開發教程(五):使用Senparc.Weixin.MP SDK
  6. Senparc.Weixin.MP SDK 微信公眾平台開發教程(六):了解MessageHandler
  7. Senparc.Weixin.MP SDK 微信公眾平台開發教程(七):解決用戶上下文(Session)問題
  8. Senparc.Weixin.MP SDK 微信公眾平台開發教程(八):通用接口說明
  9. Senparc.Weixin.MP SDK 微信公眾平台開發教程(九):自定義菜單接口說明
  10. Senparc.Weixin.MP SDK 微信公眾平台開發教程(十):多客服接口說明
  11. Senparc.Weixin.MP SDK 微信公眾平台開發教程(十一):高級接口說明
  12. Senparc.Weixin.MP SDK 微信公眾平台開發教程(十二):OAuth2.0說明
  13. Senparc.Weixin.MP SDK 微信公眾平台開發教程(十三):地圖相關接口說明
  14. Senparc.Weixin.MP SDK 微信公眾平台開發教程(十四):請求消息去重
  15. Senparc.Weixin.MP SDK 微信公眾平台開發教程(十五):消息加密
  16. Senparc.Weixin.MP SDK 微信公眾平台開發教程(十六):AccessToken自動管理機制
  17. Senparc.Weixin.MP SDK 微信公眾平台開發教程(十七):個性化菜單接口說明
  18. Senparc.Weixin.MP SDK 微信公眾平台開發教程(十八):Web代理功能
  19. Senparc.Weixin.MP SDK 微信公眾平台開發教程(十九):MessageHandler 的未知類型消息處理
  20. Senparc.Weixin.MP SDK 微信公眾平台開發教程(二十):使用菜單消息功能
  21. Senparc.Weixin.MP SDK 微信公眾平台開發教程(二十一):在小程序中使用 WebSocket (.NET Core)
  22. Senparc.Weixin.MP SDK 微信公眾平台開發教程(二十二):如何安裝 Nuget(dll) 后使用項目源代碼調試

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM