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实践中对消息进行处理:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
/// <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地址" , }); |
1
|
return responseMessage;<br> } |
实际的开发过程中,除了输出位置的信息,我们还可以根据用户的当前位置,检索就近的点,在Articles中输出,并计算出距离。
系列教程索引:http://www.cnblogs.com/szw/archive/2013/05/14/weixin-course-index.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)