当前位置:Gxlcms > JavaScript > 微信小程序+腾讯地图开发实现路径规划绘制

微信小程序+腾讯地图开发实现路径规划绘制

时间:2021-07-01 10:21:17 帮助过:168人阅读

现象

我们想用微信小程序实现在map>组件上自定义显示导航路径,但是目前为止官方并未给出相应的方法实现,map>组件确实有绘制点对点连线的属性polyline,但是呢我们没有一系列的坐标集合也是画不出一条路径的,

更糟糕的是即便你内置微信小程序JavaScript SDK,它目前为止也不能给你相应的返回导航路径中所有坐标集合方法实现,不信你看介绍

解决方案

那我们只能用WebService API咯,

但是不要高兴的太早,根据文档,我们要的接口参数是酱紫的

那么我们开始写(下面是菜鸡版代码,非礼勿视)

wx.request()参数的data部分对”from”/”to”赋值不能采用惯用手法了,你会发现换了好几种方式依然不能如意,要么请求参数非法,要么语法编译又过不了,没办法,最后只能使用绝招了

 

哼哼,状态稳如老狗 @_@

ok,到此为止,我们已经拿到我们想要的坐标集合了,那么接下来就是提取坐标数组,然后给polyline绘制的问题了

利用polyline绘制路径

什么都不说了,直接上代码:

  1. /**
  2. * 获取当前位置标记在地图上并且利用polyline绘制路径
  3. */
  4. now_LocationTap: function () {
  5. var that = this
  6. /**
  7. * 初始化当前地图标记信息
  8. */
  9. wx.getLocation({
  10. type: 'gcj02', // 默认为 wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标
  11. success: function (res) {
  12. console.log('当前位置数据Object如下:')
  13. console.log(res)
  14. /** 开始同步数据 */
  15. that.setData({
  16. now_Location: {
  17. latitude: res.latitude,
  18. longitude: res.longitude,
  19. },
  20. /**初始化地图标记点附近车辆信息 */
  21. markers: [
  22. {
  23. iconPath: '/resources/wait/car.png',
  24. width: 70,
  25. height: 70,
  26. latitude: res.latitude,
  27. longitude: res.longitude
  28. }
  29. ]
  30. })
  31. console.log('当前latitude:' + res.latitude)
  32. console.log('当前longitude:' + res.longitude)
  33. console.log('当前latitude同步结果:' + that.data.now_Location.latitude)
  34. console.log('当前longitude同步结果:' + that.data.now_Location.longitude)
  35. /********************** 从腾讯地图WebService API请求导航路线坐标集合用于point_Array折线连接 */
  36. var now_Location = String(res.latitude + ',' + res.longitude)
  37. var end_Location = String(that.data.endLocation.latitude + ',' + that.data.endLocation.longitude)
  38. wx.request({
  39. url: 'https://apis.map.qq.com/ws/direction/v1/driving/', //连接接口地址
  40. data: {
  41. from: now_Location,
  42. to: end_Location,
  43. policy: 'REAL_TRAFFIC', //结合路况的最优方式
  44. key: '腾讯地图KEY',
  45. },
  46. header: {
  47. 'content-type': 'application/json' // 默认值
  48. },
  49. success: function (res) {
  50. console.log(res.data)
  51. console.log('剩余距离:' + res.data.result.routes[0].distance + '米')
  52. console.log('剩余时间:' + res.data.result.routes[0].duration + '分钟')
  53. console.log('导航路线点串如下:')
  54. console.log(res.data.result.routes[0].polyline)
  55. /** 获取返回的方案路线坐标点串并解压 */
  56. var coors = res.data.result.routes[0].polyline
  57. for (var i = 2; i < coors.length; i++)
  58. { coors[i] = coors[i - 2] + coors[i] / 1000000 }
  59. console.log('路线坐标点串解压完毕!')
  60. console.log('路线坐标点串解压结果如下:')
  61. console.log(coors);
  62. /** 将解压后的坐标点串进行拼接成polyline想要的样子 */
  63. var coors_new=[] //记住微信小程序声明一个数组这样写
  64. for(var j = 0; j < coors.length; j++){
  65. coors_new.push({
  66. latitude: parseFloat(coors[j]),
  67. longitude: parseFloat(coors[j+1])
  68. })
  69. j++;
  70. }
  71. /* 自己想的针对polyline的points做出的格式化方案,直接实现了目标对象的字符串形式,但是一开始没注意数据类型的问题,随后试了好几种方案都不如意,最终查看了高德地图的开发方案后恍然大悟,Array.push({latitude:'',longitude:''}),简直完美!
  72. for (var i = 0, j = 0; i < coors.length - 2, j < coors.length/2; i++,j++)
  73. {
  74. coors[i] = String('{latitude:'+String(coors[i])+','+'longitude:'+String(coors[i + 1])+'}') ;
  75. coors_new[j] = coors[i];
  76. i+=1; //此处注意不+2的原因是:还有for循环的自动+1,结合起来就会达到跳2的效果
  77. }
  78. */
  79. console.log('路线坐标点串格式化完毕!')
  80. console.log('路线坐标点串格式化结果如下:')
  81. console.log(coors)
  82. console.log('已经产生新的经纬度数组coors_new如下:')
  83. console.log(coors_new)
  84. console.log('路线坐标点串解压后一共:' + coors.length + '个')
  85. console.log('路线坐标点串转换后一共:' + coors_new.length + '个')
  86. /** 开始同步路线坐标集合+剩余距离+剩余时间数据 */
  87. that.setData({
  88. now_Duration: res.data.result.routes[0].duration, //剩余时间
  89. now_Distance: res.data.result.routes[0].distance, //剩余距离
  90. polyline_Object: [{
  91. points: coors_new,//指定一系列坐标点,从数组第一项连线至最后一项
  92. color: "#FF0000DD",
  93. width: 2,
  94. dottedLine: true
  95. }],
  96. })
  97. console.log('更新points经纬度数组如下:')
  98. console.log(that.data.polyline_Object[0].points)
  99. console.log('更新polyline_Object如下:')
  100. console.log(that.data.polyline_Object)
  101. console.log('当前剩余时间 now_Duration同步结果:' + that.data.now_Duration+'分钟')
  102. console.log('当前剩余距离now_Distance同步结果:' + that.data.now_Distance+'米')
  103. }
  104. })
  105. },
  106. })
  107. }!

 

至此路径规划告一段落

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

人气教程排行