xbd-maptrack.vue 3.5 KB

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