playMap.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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: 2000rpx" @updated="test" />
  5. <button v-if="startMove" @click="handleStopMove()">暂停移动</button>
  6. <button v-else @click="handleStartMove()">开始移动</button>
  7. <u-toast ref="uToast"></u-toast>
  8. </view>
  9. </template>
  10. <script>
  11. // const img = '/static/logo.png';
  12. const img = '/static/live-camera/shutter.png';
  13. import * as config from '@/config'
  14. let baseUrl = config.def().baseUrlNew
  15. export default {
  16. data() {
  17. return {
  18. scaleNum: 10,
  19. mapContext: null, //地图对象
  20. startMove: false, //是否开始回放
  21. nextPointIndex: 1, //下一个坐标点的索引
  22. durationTime: 1000, //相邻两点动画持续时长默认1秒
  23. //路线信息
  24. polyline: [{
  25. width: 20,
  26. points: [],
  27. arrowLine: true,
  28. color: '#3591FC',
  29. }],
  30. //标记点(即移动标记物)
  31. markers: [{
  32. id: 1,
  33. width: 40,
  34. height: 40,
  35. latitude: 0,
  36. longitude: 0,
  37. iconPath: img,
  38. anchor: {
  39. x: 0.5,
  40. y: 1
  41. }
  42. }],
  43. infoData: {},
  44. content: "",
  45. show: false,
  46. obj: {}
  47. }
  48. },
  49. onLoad(option) {
  50. this.getTrack(option.startDate, option.endDate) //获取轨迹信息(只做演示,未进行远程请求)
  51. },
  52. methods: {
  53. //模拟获取远程数据
  54. getTrack(startDate, endDate) {
  55. uni.request({
  56. url: baseUrl + '/CarPostionController/api/gettrack',
  57. data: {
  58. startDate: startDate,
  59. endDate: endDate
  60. },
  61. method: 'post',
  62. header: {
  63. 'content-type': 'application/json' //'application/x-www-form-urlencoded; charset=UTF-8',
  64. },
  65. success: (res) => {
  66. console.log("res", res)
  67. if (res.data.code == 200) {
  68. let _list = JSON.parse(res.data.data)
  69. this.latitude = _list[0].lat
  70. this.longitude = _list[0].lng
  71. this.markers[0].latitude = this.latitude;
  72. this.markers[0].longitude = this.longitude;
  73. console.log("_list", _list)
  74. this.polyline[0].points = []
  75. for (let i = 0; i < _list.length; i++) {
  76. this.polyline[0].points.push({
  77. latitude: _list[i].lat,
  78. longitude: _list[i].lng
  79. })
  80. }
  81. console.log(this.polyline)
  82. var that = this
  83. setTimeout(() => {
  84. that.durationTime = Math.ceil(30000 / that.polyline[0].points
  85. .length) //默认播放全程使用30秒,计算相连两点动画时长
  86. that.initMapData()
  87. }, 1000)
  88. }
  89. }
  90. })
  91. },
  92. //设置地图
  93. initMapData() {
  94. this.initMarkers()
  95. this.mapContext = uni.createMapContext('myMap', this)
  96. },
  97. test() {
  98. this.mapContext.includePoints({
  99. points: this.polyline[0].points,
  100. padding: [100, 100, 1000, 100]
  101. })
  102. },
  103. //设置位置(从起点开始)
  104. initMarkers() {
  105. this.markers[0].latitude = this.polyline[0].points[0].latitude
  106. this.markers[0].longitude = this.polyline[0].points[0].longitude
  107. },
  108. //开始移动
  109. handleStartMove() {
  110. this.startMove = true
  111. this.movePoint()
  112. },
  113. //停止移动
  114. handleStopMove() {
  115. this.startMove = false
  116. },
  117. //移动坐标
  118. movePoint() {
  119. /*
  120. //也可以用这个方法
  121. this.mapContext.moveAlong({
  122. duration: 30000,
  123. markerId: this.markers[0].id,
  124. path: this.polyline[0].points
  125. })
  126. return
  127. */
  128. this.mapContext.moveAlong({
  129. duration: 10000,
  130. markerId: this.markers[0].id,
  131. path: this.polyline[0].points
  132. })
  133. console.log("this.nextPointIndex1 ", this.nextPointIndex, this.polyline[0].points.length - 1)
  134. console.log("this.startMove1", this.startMove)
  135. // this.mapContext.translateMarker({
  136. // duration: this.durationTime,
  137. // markerId: this.markers[0].id,
  138. // destination: {
  139. // latitude: this.polyline[0].points[this.nextPointIndex].latitude,
  140. // longitude: this.polyline[0].points[this.nextPointIndex].longitude
  141. // },
  142. // animationEnd: res => {
  143. // console.log("this.nextPointIndex ",this.nextPointIndex ,this.polyline[0].points.length - 1)
  144. // console.log("this.startMove",this.startMove)
  145. // //播放结束,继续移动到下一个点,最后一个点时结束移动
  146. // if (this.nextPointIndex < this.polyline[0].points.length - 1) {
  147. // this.nextPointIndex++
  148. // if (this.startMove) {
  149. // this.movePoint()
  150. // }
  151. // } else {
  152. // this.nextPointIndex = 1
  153. // this.startMove = false
  154. // }
  155. // }
  156. // })
  157. }
  158. }
  159. };
  160. </script>
  161. <style lang="scss" scoped>
  162. .hcp-bottom {
  163. left: 0;
  164. bottom: 0;
  165. width: 750rpx;
  166. // position: fixed;
  167. }
  168. .content {
  169. .top_btn {
  170. // background-color: red;
  171. position: fixed;
  172. top: 30rpx;
  173. left: 10px;
  174. right: 30rpx;
  175. flex-direction: row;
  176. display: flex;
  177. margin-top: 30px;
  178. justify-content: space-between;
  179. .returnPage {
  180. flex-direction: row;
  181. display: flex;
  182. .return-icon {
  183. width: 90rpx;
  184. height: 90rpx;
  185. }
  186. }
  187. .qiehuan {
  188. flex-direction: row;
  189. display: flex;
  190. // width: 240rpx;
  191. height: 90rpx;
  192. background-color: #ffffff;
  193. border-radius: 50rpx;
  194. padding: 0 30rpx;
  195. .tips {
  196. line-height: 90rpx;
  197. }
  198. .qiehuan-icon {
  199. width: 30rpx;
  200. height: 30rpx;
  201. margin-top: 30rpx;
  202. margin-right: 10rpx;
  203. }
  204. }
  205. }
  206. .infoBox {
  207. // width: 90%;
  208. height: 260rpx;
  209. background-color: #FFFFFF;
  210. position: fixed;
  211. top: 30rpx;
  212. left: 10px;
  213. right: 30rpx;
  214. border-radius: 40rpx;
  215. // padding:50rpx;
  216. display: flex;
  217. flex-direction: row;
  218. margin-top: 100px;
  219. .infoBox_left {
  220. padding-top: 50rpx;
  221. width: 250rpx;
  222. padding-top: 80rpx;
  223. // background-color: #18BC37;
  224. }
  225. .infoBox_right {
  226. background-image: linear-gradient(to right, #f9f7f7, #ffffff);
  227. display: flex;
  228. flex-direction: row;
  229. border-bottom-right-radius: 40rpx;
  230. border-top-right-radius: 40rpx;
  231. .jt-icon {
  232. position: relative;
  233. top: 75rpx;
  234. width: 60rpx;
  235. margin: auto 10rpx;
  236. }
  237. .send {
  238. padding-top: 80rpx;
  239. width: 180rpx;
  240. // background-color: #0077AA;
  241. }
  242. .end {
  243. padding-top: 80rpx;
  244. width: 180rpx;
  245. }
  246. }
  247. .title {
  248. font-size: 44rpx;
  249. font-weight: 600;
  250. text-align: center;
  251. }
  252. .address {
  253. text-align: center;
  254. color: #8F8F8F;
  255. font-size: 30rpx;
  256. margin-top: 15rpx;
  257. }
  258. .estimate {
  259. width: 120px;
  260. text-align: center;
  261. }
  262. }
  263. }
  264. </style>