xbdMaptrack.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <template>
  2. <view class="content">
  3. <map style="width: 100%; height: 100%;position: fixed;" :markers="covers" :polyline="polyline"
  4. :include-points="covers" @markertap="showRoute" id="myMap" show-location></map>
  5. <slider :value="progress" @change="sliderChange" @changing="trackState = 2" activeColor="#FFD090"
  6. block-color="#D65600" />
  7. <view class="fn">
  8. <button @tap="trackState = trackState == 1 ? 2 : 1">{{ ['开始', '暂停', '继续'][trackState] }}</button>
  9. <button @tap="trackState = 0">终止</button>
  10. </view>
  11. </view>
  12. </template>
  13. <script>
  14. export default {
  15. name: 'xbdMaptrack',
  16. props: ['isauto', 'covers', 'polyline'], //是否自动播放和轨迹数据
  17. data() {
  18. return {
  19. trackLength: 0, //轨迹长度
  20. trackkey: 0, //轨迹点
  21. progress: 0, // 进度条
  22. mapContext: '',
  23. terval: false, //计时器
  24. trackState: 0 //轨迹播放状态 0未开始 1播放 2暂停
  25. };
  26. },
  27. watch: {
  28. trackState(k) {
  29. //监听播放状态改变
  30. if (k == 1 && !this.terval) return this.trackplay();
  31. clearInterval(this.terval);
  32. this.terval = false;
  33. //进度停止时返回当前轨迹的key值与坐标值
  34. this.$emit('getStopTrack', {
  35. key: this.trackkey,
  36. track: this.polyline[0].points[this.trackkey]
  37. })
  38. if (k !== 0) return;
  39. this.trackkey = 0;
  40. this.progress = 0;
  41. this.trackMove();
  42. },
  43. },
  44. created() {
  45. this.mapContext = uni.createMapContext('myMap', this);
  46. setTimeout(() => {
  47. //mapContext 创建后延时设置定位
  48. this.trackLength = this.polyline[0].points.length - 1;
  49. this.mapContext.moveToLocation(this.covers[0]);
  50. if (this.isauto) this.trackState = 1;
  51. }, 888);
  52. },
  53. methods: {
  54. showRoute() {},
  55. /**
  56. * 轨迹播放方法
  57. * @param {number} s = 10 控制移动速度
  58. *
  59. * */
  60. trackplay(s = 10) {
  61. this.terval = setInterval(() => {
  62. this.trackkey++;
  63. if (this.trackkey >= this.trackLength) {
  64. clearInterval(this.terval);
  65. this.trackState = 0;
  66. this.trackkey = 0;
  67. this.terval = false;
  68. }
  69. this.progress = ((this.trackkey * 100) / this.trackLength).toFixed(1);
  70. this.trackMove();
  71. }, s);
  72. },
  73. /**
  74. * 轨迹移动方法
  75. * @param {number} markerId = 2 指定 marker的id
  76. * @param {object} destination ={latitude,longitude} 指定 marker 移动到的目标点
  77. * @param {number} duration = 100 动画持续时长
  78. * @param {void} animationEnd = ()=>{} 轨迹动画完成后回调
  79. * @param {void} fail 轨迹回放失败回调
  80. *
  81. * */
  82. trackMove(markerId = 2, destination = this.polyline[0].points[this.trackkey], duration = 100, animationEnd =
  83. () => {}, fail = e => console.log('轨迹回放失败', e)) {
  84. this.mapContext.translateMarker({
  85. markerId,
  86. destination,
  87. duration,
  88. animationEnd,
  89. fail
  90. });
  91. },
  92. //进度条改变了调用方法
  93. sliderChange(e) {
  94. this.trackkey = ((e.detail.value / 100) * this.trackLength).toFixed(0);
  95. //进度条改变时返回当前轨迹的key值与坐标值
  96. this.$emit('getStopTrack', {
  97. key: this.trackkey,
  98. track: this.polyline[0].points[this.trackkey]
  99. })
  100. }
  101. }
  102. };
  103. </script>
  104. <style>
  105. .content {
  106. display: flex;
  107. flex-direction: column;
  108. align-items: center;
  109. justify-content: center;
  110. }
  111. .logo {
  112. height: 200rpx;
  113. width: 200rpx;
  114. margin-top: 200rpx;
  115. margin-left: auto;
  116. margin-right: auto;
  117. margin-bottom: 50rpx;
  118. }
  119. .text-area {
  120. display: flex;
  121. justify-content: center;
  122. }
  123. .fn {
  124. position: fixed;
  125. bottom: 22%;
  126. text-align: center;
  127. z-index: 100;
  128. }
  129. .title {
  130. font-size: 36rpx;
  131. color: #8f8f94;
  132. }
  133. slider {
  134. width: 88%;
  135. height: 12px;
  136. margin-left: 2.5%;
  137. }
  138. </style>