博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Senparc.Weixin.MP SDK 微信公众平台开发教程(十三):地图相关接口说明
阅读量:6995 次
发布时间:2019-06-27

本文共 3039 字,大约阅读时间需要 10 分钟。

  为了方便大家开发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实践中对消息进行处理:

     ///         /// 处理位置请求        ///         ///         /// 
public override IResponseMessageBase OnLocationRequest(RequestMessageLocation requestMessage) { var responseMessage = ResponseMessageBase.CreateFromRequestMessage
(requestMessage); var markersList = new List
(); 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://thivl.baihongyu.com/

你可能感兴趣的文章
Dapper基础知识一
查看>>
maven安装
查看>>
简单综合实验(企业局域网)
查看>>
MyBatis学习总结(9)——使用MyBatis Generator自动创建代码
查看>>
Java基础学习总结(17)——线程
查看>>
MariaDB快速入门指南
查看>>
MySQL密码管理、存储引擎及事务
查看>>
Spring学习总结(5)——IOC注入方式总结
查看>>
Maven学习总结(一)——Maven入门
查看>>
Hibernate工具类
查看>>
<org manual>翻译--激活
查看>>
Laravel 使用 Aliyun OSS 云存储
查看>>
C:\Windows\system32\MFC42u.dll没有被指定在Windows上运行?
查看>>
OpenStack Swift client开发
查看>>
Windows Sever 2012R2 部署SCDPM 2012R2 (3)---安装
查看>>
pfSense调整和解决部分网卡问题
查看>>
CentOS中×××的配置安装 (四)PPTP-×××多用户多出口接入配置
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
我的友情链接
查看>>