map.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. this.id = option.id
  57. this.getTrack() //获取轨迹信息(只做演示,未进行远程请求)
  58. },
  59. methods: {
  60. //模拟获取远程数据
  61. getTrack() {
  62. this.$request.baseRequest('post', '//hyOrderTravelPath/getInfo', {
  63. orderId: this.id,
  64. }).then(res => {
  65. console.log("res",res)
  66. // this.polyline[0].points = [
  67. // {latitude: 39.997761, longitude: 116.478935},
  68. // {latitude: 39.997825, longitude: 116.478939},
  69. // {latitude: 39.998549, longitude: 116.478912},
  70. // {latitude: 39.998555, longitude: 116.478998},
  71. // {latitude: 39.998566, longitude: 116.479282},
  72. // {latitude: 39.998528, longitude: 116.479658},
  73. // {latitude: 39.998453, longitude: 116.480151},
  74. // {latitude: 39.998302, longitude: 116.480784},
  75. // {latitude: 39.998184, longitude: 116.481149},
  76. // {latitude: 39.997997, longitude: 116.481573},
  77. // {latitude: 39.997846, longitude: 116.481863},
  78. // {latitude: 39.997718, longitude: 116.482072},
  79. // {latitude: 39.997718, longitude: 116.482362},
  80. // {latitude: 39.998935, longitude: 116.483633},
  81. // {latitude: 39.998968, longitude: 116.48367},
  82. // {latitude: 39.999861, longitude: 116.484648}
  83. // ]
  84. // this.durationTime = Math.ceil(30000 / this.polyline[0].points.length) //默认播放全程使用30秒,计算相连两点动画时长
  85. // this.initMapData()
  86. })
  87. .catch(res => {
  88. uni.hideLoading()
  89. uni.showToast({
  90. title: res.message,
  91. icon: 'none',
  92. duration: 2000
  93. })
  94. });
  95. },
  96. //设置地图
  97. initMapData() {
  98. this.initMarkers()
  99. this.mapContext = uni.createMapContext('myMap', this)
  100. },
  101. //设置位置(从起点开始)
  102. initMarkers() {
  103. this.markers[0].latitude = this.polyline[0].points[0].latitude
  104. this.markers[0].longitude = this.polyline[0].points[0].longitude
  105. },
  106. //开始移动
  107. handleStartMove() {
  108. this.startMove = true
  109. this.movePoint()
  110. },
  111. //停止移动
  112. handleStopMove() {
  113. this.startMove = false
  114. },
  115. //移动坐标
  116. movePoint() {
  117. /*
  118. //也可以用这个方法
  119. this.mapContext.moveAlong({
  120. duration: 30000,
  121. markerId: this.markers[0].id,
  122. path: this.polyline[0].points
  123. })
  124. return
  125. */
  126. // this.mapContext.moveAlong({
  127. // duration: 10000,
  128. // markerId: this.markers[0].id,
  129. // path: this.polyline[0].points
  130. // })
  131. console.log("this.nextPointIndex1 ",this.nextPointIndex ,this.polyline[0].points.length - 1)
  132. console.log("this.startMove1",this.startMove)
  133. this.mapContext.translateMarker({
  134. duration: this.durationTime,
  135. markerId: this.markers[0].id,
  136. destination: {
  137. latitude: this.polyline[0].points[this.nextPointIndex].latitude,
  138. longitude: this.polyline[0].points[this.nextPointIndex].longitude
  139. },
  140. animationEnd: res => {
  141. console.log("this.nextPointIndex ",this.nextPointIndex ,this.polyline[0].points.length - 1)
  142. console.log("this.startMove",this.startMove)
  143. //播放结束,继续移动到下一个点,最后一个点时结束移动
  144. if (this.nextPointIndex < this.polyline[0].points.length - 1) {
  145. this.nextPointIndex++
  146. if (this.startMove) {
  147. this.movePoint()
  148. }
  149. } else {
  150. this.nextPointIndex = 1
  151. this.startMove = false
  152. }
  153. }
  154. })
  155. }
  156. }
  157. };
  158. </script>
  159. <style lang="scss" scoped>
  160. .hcp-bottom {
  161. left: 0;
  162. bottom: 0;
  163. width: 750rpx;
  164. position: fixed;
  165. }
  166. </style>