map.vue 4.5 KB

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