use-g-map-trajectory.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { getRoot } from '/@/root'
  2. import startSrc from '/@/assets/icons/start.svg'
  3. import endSrc from '/@/assets/icons/end.svg'
  4. export function useGMapTrajectory() {
  5. const root = getRoot();
  6. let AMap = root.$aMap;
  7. // 绘制轨迹
  8. const drawTrajectory = (paths: any[]) => {
  9. // 绘制起点图标
  10. const startPosition = paths[0];
  11. const startIcon = new AMap.Icon({
  12. size: new AMap.Size(40, 40),
  13. image: startSrc,
  14. imageSize: new AMap.Size(40, 40)
  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 endPosition = paths[paths.length - 1];
  23. const endIcon = new AMap.Icon({
  24. size: new AMap.Size(40, 40),
  25. image: endSrc,
  26. imageSize: new AMap.Size(40, 40)
  27. })
  28. const endMarker = new AMap.Marker({
  29. position: new AMap.LngLat(endPosition[0], endPosition[endPosition.length - 1]),
  30. icon: endIcon,
  31. offset: new AMap.Pixel(-20, -35)
  32. })
  33. // 绘制轨迹折线
  34. const polyline = new AMap.Polyline({
  35. path: paths,
  36. strokeColor: '#2D8CF0',
  37. strokeOpacity: 1,
  38. strokeWeight: 2,
  39. strokeStyle: 'solid',
  40. })
  41. root.$map.add([startMarker, endMarker, polyline]);
  42. // 自动缩放地图到合适的视野级别
  43. root.$map.setFitView([startMarker, endMarker, polyline]);
  44. }
  45. return {
  46. drawTrajectory
  47. }
  48. }