map.nvue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <template>
  2. <view>
  3. <map v-if="polyline[0].points.length > 0" ref="myMap" id="myMap" :markers="markers" :polyline="polyline"
  4. :latitude="polyline[0].points[0].latitude" :longitude="polyline[0].points[0].longitude"
  5. style="width: 100%; height: 2000rpx" @updated="test" />
  6. <!-- <view v-if="polyline[0].points.length > 0" 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. import * as config from '@/config'
  15. let baseUrl = config.def().baseUrlNew
  16. export default {
  17. data() {
  18. return {
  19. scaleNum: 10,
  20. mapContext: null, //地图对象
  21. startMove: false, //是否开始回放
  22. nextPointIndex: 1, //下一个坐标点的索引
  23. durationTime: 1000, //相邻两点动画持续时长默认1秒
  24. //路线信息
  25. polyline: [{
  26. width: 20,
  27. points: [],
  28. arrowLine: true,
  29. color: '#3591FC',
  30. }],
  31. //标记点(即移动标记物)
  32. markers: [{
  33. id: 1,
  34. width: 40,
  35. height: 40,
  36. latitude: 0,
  37. longitude: 0,
  38. iconPath: img,
  39. anchor: {
  40. x: 0.5,
  41. y: 1
  42. }
  43. }]
  44. }
  45. },
  46. onLoad(option) {
  47. this.id = option.id
  48. console.log(this.id)
  49. this.getTrack() //获取轨迹信息(只做演示,未进行远程请求)
  50. },
  51. methods: {
  52. //模拟获取远程数据
  53. getTrack() {
  54. uni.request({
  55. url: baseUrl + '/hyOrderTravelPath/getInfo',
  56. data: {
  57. orderId: this.id,
  58. currentPage: 1,
  59. pageSize: 9999
  60. },
  61. method: 'get',
  62. header: {
  63. 'content-type': 'application/json' //'application/x-www-form-urlencoded; charset=UTF-8',
  64. },
  65. success: (res) => {
  66. if (res.data.code == 200) {
  67. if (res.data.data) {
  68. var patharr = []
  69. for (let num = 0; num < res.data.data.records.length; num++) {
  70. if(res.data.data.records[num].longitudeLatitude){
  71. patharr.push(JSON.parse(res.data.data.records[num].longitudeLatitude));
  72. }
  73. // for (let i = 0; i < patharr.length; i++) {
  74. // that.path2.push([patharr[i].longitude, patharr[i].latitude]);
  75. // }
  76. }
  77. this.polyline[0].points = patharr
  78. // if (res.data.data.longitudeLatitude) {
  79. // // console.log(JSON.parse(res.data.data.longitudeLatitude));
  80. // this.polyline[0].points = JSON.parse(res.data.data.longitudeLatitude)
  81. // }
  82. } else {
  83. uni.showToast({
  84. title: '暂无运输轨迹',
  85. icon: 'none',
  86. duration: 2000
  87. })
  88. }
  89. }
  90. }
  91. })
  92. // this.$request.baseRequest('post', '/hyOrderTravelPath/getInfo', {
  93. // orderId: this.id,
  94. // }).then(res => {
  95. // console.log("res",res)
  96. // // this.polyline[0].points = [
  97. // // {latitude: 39.997761, longitude: 116.478935},
  98. // // {latitude: 39.997825, longitude: 116.478939},
  99. // // {latitude: 39.998549, longitude: 116.478912},
  100. // // {latitude: 39.998555, longitude: 116.478998},
  101. // // {latitude: 39.998566, longitude: 116.479282},
  102. // // {latitude: 39.998528, longitude: 116.479658},
  103. // // {latitude: 39.998453, longitude: 116.480151},
  104. // // {latitude: 39.998302, longitude: 116.480784},
  105. // // {latitude: 39.998184, longitude: 116.481149},
  106. // // {latitude: 39.997997, longitude: 116.481573},
  107. // // {latitude: 39.997846, longitude: 116.481863},
  108. // // {latitude: 39.997718, longitude: 116.482072},
  109. // // {latitude: 39.997718, longitude: 116.482362},
  110. // // {latitude: 39.998935, longitude: 116.483633},
  111. // // {latitude: 39.998968, longitude: 116.48367},
  112. // // {latitude: 39.999861, longitude: 116.484648}
  113. // // ]
  114. // // for(var i =0 ;i<7200;i++){
  115. // // var latitude = this.polyline[0].points[this.polyline[0].points.length -1].latitude + 0.0001
  116. // // var longitude = this.polyline[0].points[this.polyline[0].points.length -1].longitude + 0.0001
  117. // // this.polyline[0].points.push({latitude,longitude})
  118. // // }
  119. var that = this
  120. setTimeout(() => {
  121. that.durationTime = Math.ceil(30000 / that.polyline[0].points.length) //默认播放全程使用30秒,计算相连两点动画时长
  122. that.initMapData()
  123. }, 1000)
  124. // })
  125. // .catch(res => {
  126. // uni.hideLoading()
  127. // uni.showToast({
  128. // title: res.message,
  129. // icon: 'none',
  130. // duration: 2000
  131. // })
  132. // });
  133. },
  134. //设置地图
  135. initMapData() {
  136. this.initMarkers()
  137. this.mapContext = uni.createMapContext('myMap', this)
  138. },
  139. test() {
  140. this.mapContext.includePoints({
  141. points: this.polyline[0].points,
  142. padding: [100, 100, 1000, 100]
  143. })
  144. },
  145. //设置位置(从起点开始)
  146. initMarkers() {
  147. console.log(this.polyline[0])
  148. this.markers[0].latitude = this.polyline[0].points[0].latitude
  149. this.markers[0].longitude = this.polyline[0].points[0].longitude
  150. },
  151. //开始移动
  152. handleStartMove() {
  153. this.startMove = true
  154. this.movePoint()
  155. },
  156. //停止移动
  157. handleStopMove() {
  158. this.startMove = false
  159. },
  160. //移动坐标
  161. movePoint() {
  162. /*
  163. //也可以用这个方法
  164. this.mapContext.moveAlong({
  165. duration: 30000,
  166. markerId: this.markers[0].id,
  167. path: this.polyline[0].points
  168. })
  169. return
  170. */
  171. this.mapContext.moveAlong({
  172. duration: 10000,
  173. markerId: this.markers[0].id,
  174. path: this.polyline[0].points
  175. })
  176. console.log("this.nextPointIndex1 ", this.nextPointIndex, this.polyline[0].points.length - 1)
  177. console.log("this.startMove1", this.startMove)
  178. // this.mapContext.translateMarker({
  179. // duration: this.durationTime,
  180. // markerId: this.markers[0].id,
  181. // destination: {
  182. // latitude: this.polyline[0].points[this.nextPointIndex].latitude,
  183. // longitude: this.polyline[0].points[this.nextPointIndex].longitude
  184. // },
  185. // animationEnd: res => {
  186. // console.log("this.nextPointIndex ",this.nextPointIndex ,this.polyline[0].points.length - 1)
  187. // console.log("this.startMove",this.startMove)
  188. // //播放结束,继续移动到下一个点,最后一个点时结束移动
  189. // if (this.nextPointIndex < this.polyline[0].points.length - 1) {
  190. // this.nextPointIndex++
  191. // if (this.startMove) {
  192. // this.movePoint()
  193. // }
  194. // } else {
  195. // this.nextPointIndex = 1
  196. // this.startMove = false
  197. // }
  198. // }
  199. // })
  200. }
  201. }
  202. };
  203. </script>
  204. <style lang="scss" scoped>
  205. .hcp-bottom {
  206. left: 0;
  207. bottom: 0;
  208. width: 750rpx;
  209. position: fixed;
  210. }
  211. </style>