map.vue 4.8 KB

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