use-g-map-trajectory.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { getRoot } from '/@/root'
  2. import hardstandSrc from '/@/assets/icons/hardstand.png'
  3. export function useGMapTrajectory() {
  4. const root = getRoot();
  5. let AMap = root.$aMap;
  6. // 绘制轨迹
  7. const drawTrajectory = (list: any[]) => {
  8. const paths = list.map(item => item.paths)
  9. // 绘制起点图标
  10. const startPosition = paths[0];
  11. const startIcon = new AMap.Icon({
  12. size: new AMap.Size(30, 30),
  13. image: hardstandSrc,
  14. imageSize: new AMap.Size(30, 30)
  15. })
  16. const startMarker = new AMap.Marker({
  17. position: new AMap.LngLat(startPosition[0], startPosition[startPosition.length - 1]),
  18. icon: startIcon,
  19. offset: new AMap.Pixel(-20, -35),// 位置偏移
  20. })
  21. // 绘制轨迹折线
  22. const polyline = new AMap.Polyline({
  23. path: paths,
  24. strokeColor: '#2D8CF0',
  25. showDir: true,// 显示路线白色方向箭头
  26. strokeOpacity: 1,// 轮廓线透明度
  27. strokeWeight: 6,//线宽
  28. strokeStyle: 'solid',
  29. })
  30. // 渲染序号
  31. const serialList = list.filter(item => [2, 3].includes(item.type));
  32. const text = serialList.map((item, index) => {
  33. return new AMap.Text({
  34. position: new AMap.LngLat(item.paths[0], item.paths[1]),
  35. // offset: new AMap.Pixel(-30, -30),
  36. text: index + 1,
  37. style: {
  38. fontSize: 12,
  39. padding: 0,
  40. backgroundColor: 'transparent',
  41. borderColor: 'transparent',
  42. }
  43. })
  44. })
  45. root.$map.add([startMarker, polyline, ...text]);
  46. // 自动缩放地图到合适的视野级别
  47. root.$map.setFitView([startMarker, polyline]);
  48. }
  49. return {
  50. drawTrajectory
  51. }
  52. }