为了方便大家开发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