pathSimplifierIns.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. export default {
  2. data() {
  3. return {
  4. lineArr: [],
  5. // 巡航轨迹
  6. AMap: null,
  7. map: null,
  8. PathSimplifier: null,
  9. beforeInit: true,
  10. isPlay: true,
  11. sliderVal: 0, // 进度条
  12. times: 1, // 倍速
  13. maxSpeed: 10, // 最高倍速
  14. navgtrSpeed: 5000, // 速度
  15. isMinSpeed: true,
  16. isMaxSpeed: false,
  17. navgtr: null,
  18. pathSimplifierIns: null,
  19. actualList: [],
  20. defaultRenderOptions: {
  21. renderAllPointsIfNumberBelow: -1, // 描绘路径点,如不需要设为-1
  22. pathTolerance: 2,
  23. keyPointTolerance: 0,
  24. pathLineStyle: {
  25. lineWidth: 6,
  26. strokeStyle: '#409eff',
  27. borderWidth: 1,
  28. borderStyle: '#eeeeee',
  29. dirArrowStyle: false
  30. },
  31. pathLineHoverStyle: {
  32. lineWidth: 6,
  33. strokeStyle: '#ff0000',
  34. borderWidth: 1,
  35. borderStyle: '#cccccc',
  36. dirArrowStyle: false
  37. },
  38. dirArrowStyle: {
  39. stepSpace: 30,
  40. strokeStyle: '#ffffff',
  41. lineWidth: 2
  42. },
  43. pathLineSelectedStyle: {
  44. lineWidth: 6,
  45. strokeStyle: '#409eff',
  46. borderWidth: 1,
  47. borderStyle: '#cccccc',
  48. dirArrowStyle: true
  49. },
  50. keyPointStyle: {
  51. radius: 0,
  52. fillStyle: 'rgba(8, 126, 196, 1)',
  53. lineWidth: 1,
  54. strokeStyle: '#eeeeee'
  55. },
  56. keyPointHoverStyle: {
  57. radius: 0,
  58. fillStyle: 'rgba(0, 0, 0, 0)',
  59. lineWidth: 2,
  60. strokeStyle: '#ffa500'
  61. },
  62. keyPointOnSelectedPathLineStyle: {
  63. radius: 0,
  64. fillStyle: 'rgba(8, 126, 196, 1)',
  65. lineWidth: 2,
  66. strokeStyle: '#eeeeee'
  67. }
  68. },
  69. isCursorAtPathEnd: false,
  70. palyStayus: 0, //0->未开始 1->行驶中 2->暂停
  71. value: 0, // 进度条初始化
  72. pointIcon: require('@/assets/img/icon_dizhi@2x.png'),
  73. pointGradIcon: require('@/assets/img/icon_dizhiGrad@2x.png'),
  74. moveIcon: require('@/assets/img/icon_point_mine@2x.png'),
  75. // signMarker: null,
  76. currentPoint: null,
  77. timeValue: '',
  78. trackData: []
  79. };
  80. },
  81. methods: {
  82. initLineArr() {
  83. let _this = this;
  84. let that = this;
  85. AMapUI.load(['ui/misc/PathSimplifier'], (PathSimplifier) => {
  86. if (!PathSimplifier.supportCanvas) {
  87. alert('当前环境不支持 Canvas!');
  88. return;
  89. }
  90. // 如果已存在巡航轨迹,则删除
  91. if (window.pathSimplifierIns && that.pathSimplifierIns) {
  92. //通过该方法清空上次传入的轨迹
  93. that.pathSimplifierIns.setData([]);
  94. }
  95. // 初始化坐标点
  96. if (that.lineArr.length > 0) {
  97. that.actualList = that.lineArr;
  98. //创建一个巡航轨迹路线
  99. that.pathSimplifierIns = new PathSimplifier({
  100. zIndex: 100, //地图层级,
  101. map: that.map, //所属的地图实例
  102. //巡航路线轨迹列表
  103. getPath: (pathData, pathIndex) => {
  104. return pathData.path;
  105. },
  106. //hover每一个轨迹点,展示内容
  107. getHoverTitle: function (pathData, pathIndex, pointIndex) {
  108. return '';
  109. },
  110. //自定义样式,可设置巡航器样式,巡航轨迹样式,巡航轨迹点击、hover等不同状态下的样式,不设置则用默认样式,详情请参考api文档 renderOptions:{}
  111. //绘制路线节点
  112. renderOptions: that.defaultRenderOptions
  113. });
  114. window.pathSimplifierIns = that.pathSimplifierIns;
  115. //设置数据
  116. that.pathSimplifierIns.setData([
  117. {
  118. name: '轨迹路线',
  119. path: that.actualList
  120. }
  121. ]);
  122. that.pathSimplifierIns.setSelectedPathIndex(0);
  123. function onload() {
  124. that.pathSimplifierIns.renderLater();
  125. }
  126. function onerror(e) {
  127. console.log('图片加载失败!');
  128. }
  129. //对第一条线路(即索引 0)创建一个巡航器
  130. let image = PathSimplifier.Render.Canvas.getImageContent(that.moveIcon, onload, onerror);
  131. that.navgtr = that.pathSimplifierIns.createPathNavigator(0, {
  132. loop: false, //循环播放
  133. speed: that.navgtrSpeed, //巡航速度,单位千米/小时
  134. pathNavigatorStyle: {
  135. width: 25,
  136. height: 60,
  137. //使用图片
  138. content: image, // 自定义巡航样式
  139. strokeStyle: null,
  140. fillStyle: null,
  141. //经过路径的样式
  142. pathLinePassedStyle: {
  143. lineWidth: 6,
  144. strokeStyle: '#69f81e',
  145. dirArrowStyle: {
  146. stepSpace: 15,
  147. strokeStyle: '#FFF'
  148. }
  149. }
  150. }
  151. });
  152. that.navgtr.on('start resume', function () {
  153. that.navgtr._startTime = Date.now();
  154. that.navgtr._startDist = this.getMovedDistance();
  155. });
  156. that.navgtr.on('stop pause', function () {
  157. that.navgtr._movedTime = Date.now() - that.navgtr._startTime;
  158. that.navgtr._movedDist = this.getMovedDistance() - that.navgtr._startDist;
  159. });
  160. that.navgtr.on('move', function (data, position) {
  161. that.isCursorAtPathEnd = false;
  162. let idx = position.dataItem.pointIndex; //走到了第几个点
  163. let tail = position.tail; //至下一个节点的比例位置
  164. let totalIdx = idx + tail;
  165. let len = position.dataItem.pathData.path.length - 1;
  166. // 设置当前点位
  167. that.currentPoint = that.actualList[idx];
  168. if (idx < len - 1) {
  169. that.navgtr.setSpeed(that.navgtrSpeed * that.times);
  170. }
  171. // 进度条实时展示tail
  172. !that.isOnSlider && (that.sliderVal = (totalIdx / len) * 100);
  173. // 如果到头了,回到初始状态
  174. if (that.navgtr.isCursorAtPathEnd()) {
  175. that.isCursorAtPathEnd = true;
  176. that.initPlayBox();
  177. }
  178. });
  179. // 加载完成
  180. that.beforeInit = false;
  181. }
  182. });
  183. },
  184. initPlayBox() {
  185. // 暂停
  186. this.navgControl('pause');
  187. this.playIcon = 'start';
  188. this.isPlay = true; // 播放图标
  189. this.palyStayus = 0; // 继续状态
  190. },
  191. // 控制播放按钮
  192. navgControl(type) {
  193. if (!this.navgtr || !type) {
  194. return;
  195. }
  196. if (type === 'start' || type === 'resume') {
  197. this.isPlay = false;
  198. this.palyStayus = 2;
  199. // 如果已经到了终点,重新定位坐标
  200. if (this.isCursorAtPathEnd && this.actualList.length > 0) {
  201. this.map.setCenter(this.actualList[0]);
  202. }
  203. } else if (type === 'pause') {
  204. this.isPlay = true;
  205. this.palyStayus = 1;
  206. }
  207. this.navgtr[type]();
  208. this.map.setFitView();
  209. }
  210. // carReLocate() {
  211. // // 鼠标从滑动条抬起时,重新定位
  212. // if (this.currentPoint) {
  213. // let timeout = setTimeout(() => {
  214. // clearTimeout(timeout)
  215. // this.map.setCenter(this.currentPoint)
  216. // }, 0)
  217. // }
  218. // },
  219. },
  220. created() {
  221. // const s = document.createElement('script');
  222. // s.style = 'text/javascript';
  223. // s.src = 'https://webapi.amap.com/ui/1.1/main.js';
  224. // document.body.appendChild(s);
  225. }
  226. };