playMap.nvue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <template>
  2. <view class="content">
  3. <map ref="myMap" id="myMap" :markers="markers" :polyline="polyline" :latitude="polyline[0].points[0].latitude"
  4. :longitude="polyline[0].points[0].longitude" style="width: 100%; height:3000rpx" />
  5. <view class="place">
  6. <!-- <text v-if="startMove" @click="handleStopMove()" class="play-text">暂停播放</text> -->
  7. <text @click="handleStartMove()" class="play-text">播放轨迹</text>
  8. </view>
  9. <u-toast ref="uToast"></u-toast>
  10. </view>
  11. </template>
  12. <script>
  13. // const img = '/static/logo.png';
  14. const img = '/static/live-camera/shutter.png';
  15. import * as config from '@/config'
  16. let baseUrl = config.def().baseUrlNew
  17. export default {
  18. data() {
  19. return {
  20. scaleNum: 10,
  21. mapContext: null, //地图对象
  22. startMove: false, //是否开始回放
  23. nextPointIndex: 1, //下一个坐标点的索引
  24. durationTime: 1000, //相邻两点动画持续时长默认1秒
  25. //路线信息
  26. polyline: [{
  27. points: [], // 点集合
  28. color: '#3591fc', // 线的颜色
  29. arrowLine: true, //带箭头的线
  30. width: 6 // 线的宽度
  31. }],
  32. //标记点(即移动标记物)
  33. markers: [{
  34. id: 1,
  35. width: 40,
  36. height: 40,
  37. latitude: 0,
  38. longitude: 0,
  39. // iconPath: "../static/greenCar.png",
  40. iconPath: "https://taohaoliang.oss-cn-beijing.aliyuncs.com/app/car.png",
  41. anchor: {
  42. x: 0.5,
  43. y: 1
  44. }
  45. }],
  46. infoData: {},
  47. content: "",
  48. show: false,
  49. obj: {}
  50. }
  51. },
  52. onLoad(option) {
  53. this.getTrack(option.startDate, option.endDate) //获取轨迹信息(只做演示,未进行远程请求)
  54. },
  55. methods: {
  56. //模拟获取远程数据
  57. getTrack(startDate, endDate) {
  58. let that = this
  59. uni.request({
  60. url: baseUrl + '/CarPostionController/api/gettrack',
  61. data: {
  62. startDate: startDate,
  63. endDate: endDate
  64. },
  65. method: 'post',
  66. header: {
  67. 'content-type': 'application/json' //'application/x-www-form-urlencoded; charset=UTF-8',
  68. },
  69. success: (res) => {
  70. console.log("res", res)
  71. if (res.data.code == 200) {
  72. let _list = JSON.parse(res.data.data)
  73. that.latitude = _list[0].lat
  74. that.longitude = _list[0].lng
  75. that.markers[0].latitude = that.latitude;
  76. that.markers[0].longitude = that.longitude;
  77. console.log("_list", _list)
  78. that.polyline[0].points = []
  79. for (let i = 0; i < _list.length; i++) {
  80. that.polyline[0].points.push({
  81. latitude: _list[i].lat,
  82. longitude: _list[i].lng
  83. })
  84. }
  85. console.log(that.polyline)
  86. that.durationTime = Math.ceil(30000 / that.polyline[0].points
  87. .length) //默认播放全程使用30秒,计算相连两点动画时长
  88. that.initMapData()
  89. }
  90. }
  91. })
  92. },
  93. //设置地图
  94. initMapData() {
  95. this.initMarkers()
  96. this.mapContext = uni.createMapContext('myMap', this)
  97. },
  98. // test() {
  99. // this.mapContext.includePoints({
  100. // points: this.polyline[0].points,
  101. // padding: [100, 100, 1000, 100]
  102. // })
  103. // },
  104. //设置位置(从起点开始)
  105. initMarkers() {
  106. this.markers[0].latitude = this.polyline[0].points[0].latitude
  107. this.markers[0].longitude = this.polyline[0].points[0].longitude
  108. },
  109. //开始移动
  110. handleStartMove() {
  111. this.startMove = true
  112. this.movePoint()
  113. },
  114. //停止移动
  115. handleStopMove() {
  116. this.startMove = false
  117. },
  118. //移动坐标
  119. movePoint() {
  120. this.mapContext.includePoints({
  121. points: this.polyline[0].points,
  122. })
  123. this.mapContext.moveAlong({
  124. duration: 10000,
  125. markerId: this.markers[0].id,
  126. path: this.polyline[0].points
  127. })
  128. console.log("this.nextPointIndex1 ", this.nextPointIndex, this.polyline[0].points.length - 1)
  129. console.log("this.startMove1", this.startMove)
  130. }
  131. }
  132. };
  133. </script>
  134. <style lang="scss" scoped>
  135. .play-text {
  136. font-size: 36px;
  137. background-color: #91B4F4;
  138. color: #fff;
  139. padding: 20rpx 50rpx;
  140. border-radius: 50rpx;
  141. width: 400rpx;
  142. text-align: center;
  143. }
  144. .place {
  145. position: absolute;
  146. z-index: 999;
  147. bottom: 100rpx;
  148. left: 180rpx;
  149. }
  150. </style>