MapContainer.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <template>
  2. <div id="container"></div>
  3. </template>
  4. <script setup>
  5. import { onMounted, onUnmounted } from "vue"
  6. import AMapLoader from "@amap/amap-jsapi-loader"
  7. let props = defineProps({
  8. pathData: {
  9. type: Array,
  10. default: () => ([])
  11. },
  12. point_list: {
  13. type: Array,
  14. default: () => ([])
  15. },
  16. threeD: {
  17. type: Boolean,
  18. default: false
  19. }
  20. })
  21. let map = null
  22. onMounted(() => {
  23. AMapLoader.load({
  24. key: "d1f123693def8a412c976184daa4b60e",
  25. version: "2.0",
  26. })
  27. .then((AMap) => {
  28. map = new AMap.Map("container", {
  29. pitch: 50, //地图俯仰角度,有效范围 0 度- 83 度
  30. viewMode: props.threeD ? '3D' : '2D', //地图模式
  31. rotateEnable: true, //是否开启地图旋转交互 鼠标右键 + 鼠标画圈移动 或 键盘Ctrl + 鼠标左键画圈移动
  32. pitchEnable: true, //是否开启地图倾斜交互 鼠标右键 + 鼠标上下移动或键盘Ctrl + 鼠标左键上下移动
  33. zoom: 17.5, //初始化地图层级
  34. rotation: -15, //初始地图顺时针旋转的角度
  35. zooms: [3, 20], //地图显示的缩放级别范围
  36. center: props.pathData?.length > 0 ? props.pathData[0] : [106.5799475868821, 29.504864472181577],
  37. })
  38. // 添加折线
  39. if (props.pathData?.length > 0) {
  40. let polyline = new AMap.Polyline({
  41. path: props.pathData,
  42. strokeWeight: 2, //线条宽度
  43. strokeColor: "red", //线条颜色
  44. lineJoin: "round", //折线拐点连接处样式
  45. })
  46. map.add(polyline)
  47. const beginMarker = new AMap.Marker({
  48. position: props.pathData[0],
  49. offset: new AMap.Pixel(-24, -48),
  50. icon: "/mark/begin.png",
  51. title: "起始点"
  52. })
  53. const endMarker = new AMap.Marker({
  54. position: props.pathData[props.pathData.length - 1],
  55. offset: new AMap.Pixel(-24, -48),
  56. icon: "/mark/end.png",
  57. title: "终点"
  58. })
  59. map.add([beginMarker, endMarker])
  60. }
  61. // 添加打卡点
  62. if (props.point_list?.length > 0) {
  63. props.point_list.forEach((point, index) => {
  64. const marker = new AMap.Marker({
  65. position: [point.longtitude, point.latitude],
  66. offset: new AMap.Pixel(-24, -48),
  67. icon: "/mark/daka.png",
  68. title: `打卡点${index + 1}`
  69. })
  70. map.add(marker)
  71. })
  72. }
  73. AMap.plugin(['AMap.ControlBar', "AMap.ToolBar", "AMap.Scale"], function () {
  74. let controlBar = new AMap.ControlBar({ //控制地图旋转插件
  75. position: {
  76. right: '10px',
  77. top: '10px'
  78. }
  79. })
  80. map.addControl(controlBar)
  81. let toolbar = new AMap.ToolBar()
  82. map.addControl(toolbar)
  83. let scale = new AMap.Scale()
  84. map.addControl(scale)
  85. })
  86. })
  87. .catch((e) => {
  88. console.log(e)
  89. })
  90. })
  91. onUnmounted(() => {
  92. map?.destroy()
  93. })
  94. </script>
  95. <style scoped>
  96. #container {
  97. width: 100%;
  98. height: 550px
  99. }
  100. @media only screen and (max-width: 768px) {
  101. #container {
  102. height: 450px
  103. }
  104. }
  105. </style>