02轨迹回放LocusPlayback.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>轨迹功能</title>
  7. <script src="../assets/vue@2.7.14.js"></script>
  8. <script src="../../dist/index.js"></script>
  9. <style>
  10. body {
  11. margin: 0
  12. }
  13. #map {
  14. height: 100vh;
  15. }
  16. .control {
  17. position: absolute;
  18. top: 20px;
  19. left: 40px;
  20. padding: 10px 10px 0;
  21. background: #fff;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div id="app">
  27. <div id="map"></div>
  28. <div class="control">
  29. <button @click="_ => locus.setVisible(true)">显示</button>
  30. <button @click="_ => locus.setVisible(false)">隐藏</button>
  31. <button @click="playback">回放</button>
  32. <button @click="_ => locus.pause()">暂停</button>
  33. <button @click="_ => locus.goAhead()">继续</button>
  34. <button @click="_ => locus.next(2)">前进2秒</button>
  35. <button @click="_ => locus.prev()">后退1秒</button>
  36. <button @click="_ => locus.stop()">停止</button>
  37. <button @click="addPoints">增加多个轨迹点</button>
  38. <button @click="_ => locus.deSelectPoint()">取消轨迹点选中状态</button>
  39. <button @click="_ => locus.setColor('red')">设置轨迹颜色</button>
  40. <button @click="_ => locus.setWidth(8)">设置轨迹线宽</button>
  41. <button @click="destroy">销毁</button>
  42. <p>
  43. <input type="range" v-model="range" min="0" max="1" style="width: 80%"
  44. step="0.001" @mousedown="enableChange = true" @mouseup="enableChange = false">
  45. </p>
  46. </div>
  47. </div>
  48. </body>
  49. <script>
  50. const locus = [
  51. {longitude: 116.35, latitude: 39.9},
  52. {longitude: 116.36, latitude: 39.91},
  53. {longitude: 116.37, latitude: 39.89},
  54. {longitude: 116.38, latitude: 39.91},
  55. {longitude: 116.39, latitude: 39.89},
  56. {longitude: 116.40, latitude: 39.91},
  57. {longitude: 116.41, latitude: 39.89},
  58. {longitude: 116.42, latitude: 39.91},
  59. {longitude: 116.43, latitude: 39.89},
  60. {longitude: 116.44, latitude: 39.91},
  61. {longitude: 116.45, latitude: 39.89}
  62. ]
  63. // 将经纬度坐标转换为墨卡托投影坐标
  64. function transformProj (data) {
  65. data.forEach(item => {
  66. const coords = CTMapOl.proj.fromLonLat([item.longitude, item.latitude])
  67. item.longitude = coords[0]
  68. item.latitude = coords[1]
  69. })
  70. return data
  71. }
  72. new Vue({
  73. el: '#app',
  74. data () {
  75. return {
  76. range: 0,
  77. enableChange: false,
  78. }
  79. },
  80. watch: {
  81. range (val) {
  82. if (this.enableChange) { // 减少不必要的双向控制
  83. this.locus.moveTo(+val)
  84. }
  85. }
  86. },
  87. mounted () {
  88. this.map = new CTMapOl.extend.InitMap({
  89. domId: 'map'
  90. })
  91. this.init()
  92. },
  93. methods: {
  94. init () {
  95. // 不要在 data 中定义 locus 变量,无需响应式
  96. this.locus = new CTMapOl.extend.LocusPlayback(this.map.map, transformProj(locus), {
  97. color: 'orange',
  98. onPlay: info => {
  99. this.range = info.progress
  100. },
  101. onSelectPoint: info => {
  102. console.log('最近轨迹点:',info)
  103. }
  104. })
  105. },
  106. playback () {
  107. this.locus.playback()
  108. },
  109. addPoints () {
  110. const data = Array(6).fill('').map(_ => {
  111. return {
  112. longitude: Math.random() / 5 + 116.26,
  113. latitude: Math.random() / 5 + 39.81
  114. }
  115. })
  116. this.locus.addPoints(transformProj(data))
  117. },
  118. destroy () {
  119. this.locus.destroy()
  120. }
  121. }
  122. })
  123. </script>
  124. </html>