首页 > 学院 > 开发设计 > 正文

Senparc.Weixin.MP SDK 微信公众平台开发教程(十三):地图相关接口说明

2019-11-17 02:43:36
字体:
来源:转载
供稿:网友

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实践中对消息进行处理:

    %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%20Description%20=%20string.Format("您刚才发送了地理位置信息。Location_X:{0},Location_Y:{1},Scale:{2},标签:{3}",%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%20requestMessage.Location_X,%20requestMessage.Location_Y,%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%20requestMessage.Scale,%20requestMessage.Label),%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20PicUrl%20=%20mapUrl,%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Title%20=%20"定位地点周边地图",%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Url%20=%20mapUrl%20%20%20%20%20%20%20%20%20%20%20%20});%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%20Title%20=%20"微信公众平台SDK%20官网链接",%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Description%20=%20"Senparc.Weixin.MK%20SDK地址",%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20PicUrl%20=%20"http://weixin.senparc.com/images/logo.jpg", Url = "http://weixin.senparc.com" });
            return responseMessage; }

  实际的开发过程中,除了输出位置的信息,我们还可以根据用户的当前位置,检索就近的点,在Articles中输出,并计算出距离。

  系列教程索引:http://www.VEVb.com/szw/archive/2013/05/14/weixin-course-index.html


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表