pathSimplifierIns.js 9.3 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: 40, // 最高倍速
  14. navgtrSpeed: 1000, // 速度
  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. // signMarker: null,
  73. currentPoint: null,
  74. timeValue: '',
  75. trackData: []
  76. };
  77. },
  78. methods: {
  79. initLineArr() {
  80. let _this = this;
  81. let that = this;
  82. AMapUI.load(['ui/misc/PathSimplifier'], PathSimplifier => {
  83. if (!PathSimplifier.supportCanvas) {
  84. alert('当前环境不支持 Canvas!');
  85. return;
  86. }
  87. // 如果已存在巡航轨迹,则删除
  88. if (window.pathSimplifierIns && that.pathSimplifierIns) {
  89. //通过该方法清空上次传入的轨迹
  90. that.pathSimplifierIns.setData([]);
  91. }
  92. // 初始化坐标点
  93. if (_this.lineArr.length > 0) {
  94. that.actualList = _this.lineArr;
  95. //创建一个巡航轨迹路线
  96. that.pathSimplifierIns = new PathSimplifier({
  97. zIndex: 100, //地图层级,
  98. map: this.map, //所属的地图实例
  99. //巡航路线轨迹列表
  100. getPath: (pathData, pathIndex) => {
  101. return pathData.path;
  102. },
  103. //hover每一个轨迹点,展示内容
  104. getHoverTitle: function (pathData, pathIndex, pointIndex) {
  105. return '';
  106. },
  107. //自定义样式,可设置巡航器样式,巡航轨迹样式,巡航轨迹点击、hover等不同状态下的样式,不设置则用默认样式,详情请参考api文档 renderOptions:{}
  108. //绘制路线节点
  109. renderOptions: that.defaultRenderOptions
  110. });
  111. window.pathSimplifierIns = that.pathSimplifierIns;
  112. //设置数据
  113. that.pathSimplifierIns.setData([
  114. {
  115. name: '轨迹路线',
  116. path: that.actualList
  117. }
  118. ]);
  119. that.pathSimplifierIns.setSelectedPathIndex(0);
  120. function onload() {
  121. that.pathSimplifierIns.renderLater();
  122. }
  123. function onerror(e) {
  124. console.log('图片加载失败!');
  125. }
  126. //对第一条线路(即索引 0)创建一个巡航器
  127. let image = PathSimplifier.Render.Canvas.getImageContent('https://webapi.amap.com/images/car.png', onload, onerror);
  128. that.navgtr = that.pathSimplifierIns.createPathNavigator(0, {
  129. loop: false, //循环播放
  130. speed: that.navgtrSpeed, //巡航速度,单位千米/小时
  131. pathNavigatorStyle: {
  132. width: 20,
  133. height: 30,
  134. //使用图片
  135. content: image, // 自定义巡航样式
  136. strokeStyle: null,
  137. fillStyle: null,
  138. //经过路径的样式
  139. pathLinePassedStyle: {
  140. lineWidth: 6,
  141. strokeStyle: '#69f81e',
  142. dirArrowStyle: {
  143. stepSpace: 15,
  144. strokeStyle: '#FFF'
  145. }
  146. }
  147. }
  148. });
  149. that.navgtr.on('start resume', function () {
  150. that.navgtr._startTime = Date.now();
  151. that.navgtr._startDist = this.getMovedDistance();
  152. });
  153. that.navgtr.on('stop pause', function () {
  154. that.navgtr._movedTime = Date.now() - that.navgtr._startTime;
  155. that.navgtr._movedDist = this.getMovedDistance() - that.navgtr._startDist;
  156. });
  157. that.navgtr.on('move', function (data, position) {
  158. that.isCursorAtPathEnd = false;
  159. let idx = position.dataItem.pointIndex; //走到了第几个点
  160. let tail = position.tail; //至下一个节点的比例位置
  161. let totalIdx = idx + tail;
  162. let len = position.dataItem.pathData.path.length - 1;
  163. // 设置当前点位
  164. that.currentPoint = that.actualList[idx];
  165. if (idx < len - 1) {
  166. that.navgtr.setSpeed(that.navgtrSpeed * that.times);
  167. }
  168. // 进度条实时展示tail
  169. !that.isOnSlider && (that.sliderVal = (totalIdx / len) * 100);
  170. // 如果到头了,回到初始状态
  171. if (that.navgtr.isCursorAtPathEnd()) {
  172. that.isCursorAtPathEnd = true;
  173. that.initPlayBox();
  174. }
  175. });
  176. // 加载完成
  177. that.beforeInit = false;
  178. }
  179. });
  180. },
  181. initPlayBox() {
  182. // 暂停
  183. this.navgControl('pause');
  184. this.playIcon = 'start';
  185. this.isPlay = true; // 播放图标
  186. this.palyStayus = 0; // 继续状态
  187. },
  188. // 控制播放按钮
  189. navgControl(type) {
  190. if (!this.navgtr || !type) {
  191. return;
  192. }
  193. if (type === 'start' || type === 'resume') {
  194. this.isPlay = false;
  195. this.palyStayus = 2;
  196. // 如果已经到了终点,重新定位坐标
  197. if (this.isCursorAtPathEnd && this.actualList.length > 0) {
  198. this.map.setCenter(this.actualList[0]);
  199. }
  200. } else if (type === 'pause') {
  201. this.isPlay = true;
  202. this.palyStayus = 1;
  203. }
  204. this.navgtr[type]();
  205. this.map.setFitView();
  206. },
  207. // carReLocate() {
  208. // // 鼠标从滑动条抬起时,重新定位
  209. // if (this.currentPoint) {
  210. // let timeout = setTimeout(() => {
  211. // clearTimeout(timeout)
  212. // this.map.setCenter(this.currentPoint)
  213. // }, 0)
  214. // }
  215. // },
  216. },
  217. created() {
  218. // const s = document.createElement('script');
  219. // s.style = 'text/javascript';
  220. // s.src = 'https://webapi.amap.com/ui/1.1/main.js';
  221. // document.body.appendChild(s);
  222. }
  223. };