map.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <view>
  3. <map v-if="polyline[0].points.length > 0" id="myMap" :markers="markers" :polyline="polyline"
  4. :include-points="polyline[0].points" :latitude="polyline[0].points[0].latitude"
  5. :longitude="polyline[0].points[0].longitude" style="width: 100%; height: calc(100vh - 90px)" />
  6. <view class="hcp-bottom">
  7. <button v-if="startMove" @click="handleStopMove()">暂停移动</button>
  8. <button v-else @click="handleStartMove()">开始移动</button>
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. const img = '/static/logo.png';
  14. export default {
  15. data() {
  16. return {
  17. id: '',
  18. mapContext: null, //地图对象
  19. startMove: false, //是否开始回放
  20. nextPointIndex: 1, //下一个坐标点的索引
  21. durationTime: 1000, //相邻两点动画持续时长默认1秒
  22. //路线信息
  23. polyline: [{
  24. width: 28,
  25. points: [],
  26. arrowLine: true,
  27. color: '#3591FC',
  28. }],
  29. //标记点(即移动标记物)
  30. markers: [{
  31. id: 1,
  32. width: 140,
  33. height: 140,
  34. latitude: 0,
  35. longitude: 0,
  36. iconPath: img,
  37. anchor: {
  38. x: 0.5,
  39. y: 1
  40. }
  41. }]
  42. }
  43. },
  44. onLoad(option) {
  45. this.id = option.id
  46. console.log(option.id)
  47. this.getTrack() //获取轨迹信息(只做演示,未进行远程请求)
  48. },
  49. methods: {
  50. //模拟获取远程数据
  51. getTrack() {
  52. this.$request.baseRequest('get', '/hyOrderTravelPath/getInfo', {
  53. orderId: this.id,
  54. currentPage: 1,
  55. pageSize: 9999
  56. }).then(res => {
  57. console.log("res", res)
  58. // this.polyline[0].points = [
  59. // {latitude: 39.997761, longitude: 116.478935},
  60. // {latitude: 39.997825, longitude: 116.478939},
  61. // {latitude: 39.998549, longitude: 116.478912},
  62. // {latitude: 39.998555, longitude: 116.478998},
  63. // {latitude: 39.998566, longitude: 116.479282},
  64. // {latitude: 39.998528, longitude: 116.479658},
  65. // {latitude: 39.998453, longitude: 116.480151},
  66. // {latitude: 39.998302, longitude: 116.480784},
  67. // {latitude: 39.998184, longitude: 116.481149},
  68. // {latitude: 39.997997, longitude: 116.481573},
  69. // {latitude: 39.997846, longitude: 116.481863},
  70. // {latitude: 39.997718, longitude: 116.482072},
  71. // {latitude: 39.997718, longitude: 116.482362},
  72. // {latitude: 39.998935, longitude: 116.483633},
  73. // {latitude: 39.998968, longitude: 116.48367},
  74. // {latitude: 39.999861, longitude: 116.484648}
  75. // ]
  76. // this.durationTime = Math.ceil(30000 / this.polyline[0].points.length) //默认播放全程使用30秒,计算相连两点动画时长
  77. // this.initMapData()
  78. })
  79. .catch(res => {
  80. uni.hideLoading()
  81. uni.showToast({
  82. title: res.message,
  83. icon: 'none',
  84. duration: 2000
  85. })
  86. });
  87. },
  88. //设置地图
  89. initMapData() {
  90. this.initMarkers()
  91. this.mapContext = uni.createMapContext('myMap', this)
  92. },
  93. //设置位置(从起点开始)
  94. initMarkers() {
  95. this.markers[0].latitude = this.polyline[0].points[0].latitude
  96. this.markers[0].longitude = this.polyline[0].points[0].longitude
  97. },
  98. //开始移动
  99. handleStartMove() {
  100. this.startMove = true
  101. this.movePoint()
  102. },
  103. //停止移动
  104. handleStopMove() {
  105. this.startMove = false
  106. },
  107. //移动坐标
  108. movePoint() {
  109. /*
  110. //也可以用这个方法
  111. this.mapContext.moveAlong({
  112. duration: 30000,
  113. markerId: this.markers[0].id,
  114. path: this.polyline[0].points
  115. })
  116. return
  117. */
  118. // this.mapContext.moveAlong({
  119. // duration: 10000,
  120. // markerId: this.markers[0].id,
  121. // path: this.polyline[0].points
  122. // })
  123. console.log("this.nextPointIndex1 ", this.nextPointIndex, this.polyline[0].points.length - 1)
  124. console.log("this.startMove1", this.startMove)
  125. this.mapContext.translateMarker({
  126. duration: this.durationTime,
  127. markerId: this.markers[0].id,
  128. destination: {
  129. latitude: this.polyline[0].points[this.nextPointIndex].latitude,
  130. longitude: this.polyline[0].points[this.nextPointIndex].longitude
  131. },
  132. animationEnd: res => {
  133. console.log("this.nextPointIndex ", this.nextPointIndex, this.polyline[0].points
  134. .length - 1)
  135. console.log("this.startMove", this.startMove)
  136. //播放结束,继续移动到下一个点,最后一个点时结束移动
  137. if (this.nextPointIndex < this.polyline[0].points.length - 1) {
  138. this.nextPointIndex++
  139. if (this.startMove) {
  140. this.movePoint()
  141. }
  142. } else {
  143. this.nextPointIndex = 1
  144. this.startMove = false
  145. }
  146. }
  147. })
  148. }
  149. }
  150. };
  151. </script>
  152. <style lang="scss" scoped>
  153. .hcp-bottom {
  154. left: 0;
  155. bottom: 0;
  156. width: 750rpx;
  157. position: fixed;
  158. }
  159. </style>