1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
| <script setup> import { onMounted, onUnmounted, watch } from 'vue'; import AMapLoader from '@amap/amap-jsapi-loader'; import Icon from '@/assets/YourNowPointIcon.png'; // 当前位置图标需自行更改
let map = null; let marker = null; let nowMarker = null; let polyline = null; // 接收父组件传来的 props const props = defineProps({ zoom: { type: Number, default: 13, }, marker: { type: Object, default: null, }, startPoint: { type: Array, required: true, }, endPoint: { type: Array, required: true, }, nowPoint: { type: Array, required: true, }, });
// 获取环境变量中的高德 API key const key = import.meta.env.VITE_GAODE_API_KEY; const secrectKey = import.meta.env.VITE_GAODE_API_SECRECT_KEY;
// 转换字符串坐标为数字 const normalizePoint = (point) => { if (Array.isArray(point)) { return point.map(p => typeof p === 'string' ? parseFloat(p) : p); } return point; };
const loadMap = async () => { const start = normalizePoint(props.startPoint); const now = normalizePoint(props.nowPoint); const end = normalizePoint(props.endPoint); console.log(start) console.log(now) console.log(end) window._AMapSecurityConfig = { securityJsCode: secrectKey, };
const AMap = await AMapLoader.load({ key: key, version: '2.0', plugins: ['AMap.Scale', 'AMap.Marker'], });
map = new AMap.Map('map_container', { viewMode: '3D', zoom: props.zoom, mapStyle: "amap://styles/macaron", center: now, }); console.log("intiCenter:") console.log(map.getCenter().toJSON()) // 可选 marker if (props.marker) { marker = new AMap.Marker({ position: normalizePoint(props.marker.position), title: props.marker.title || '', }); map.add(marker); }
// 起点 map.add( new AMap.Marker({ position: start || [116.035914, 39.710443], title: "起点", }) );
// nowPoint marker const nowIcon = new AMap.Icon({ size: new AMap.Size(60, 60), image: Icon, imageSize: new AMap.Size(60, 60), }); nowMarker = new AMap.Marker({ position: now || [116.035914, 39.710443], title: "Now", icon: nowIcon, offset: new AMap.Pixel(-30, -30), }); map.add(nowMarker);
// 终点 const endIcon = new AMap.Icon({ size: new AMap.Size(25, 34), image: '//a.amap.com/jsapi_demos/static/demo-center/icons/dir-marker.png', imageSize: new AMap.Size(135, 40), imageOffset: new AMap.Pixel(-95, -3), }); map.add( new AMap.Marker({ position: end || [115.981787, 39.693467], title: "终点", icon: endIcon, offset: new AMap.Pixel(-13, -30), }) );
// 初始轨迹线(起点 -> 当前点) polyline = new AMap.Polyline({ path: [start, now], strokeWeight: 2, strokeColor: "red", lineJoin: "round", }); map.add(polyline); };
// 监听中心点变化(可选) watch( () => props.center, (newCenter) => { if (map) { map.setCenter(normalizePoint(newCenter)); if (marker) marker.setPosition(normalizePoint(newCenter)); } } );
// 监听缩放变化 watch( () => props.zoom, (newZoom) => { if (map && newZoom) map.setZoom(newZoom); } );
// 监听 nowPoint 的变化:更新 UAV marker 和折线路径 watch( () => props.nowPoint, (newNowPointRaw) => { const newNowPoint = normalizePoint(newNowPointRaw); const start = normalizePoint(props.startPoint); if (nowMarker && newNowPoint) { nowMarker.setPosition(newNowPoint); console.log("centerCheck1:") console.log(map.getCenter().toJSON()) map?.setCenter(newNowPoint);
console.log("centerCheck2:") console.log(map.getCenter().toJSON()) // 更新折线 if (polyline) { polyline.setPath([start, newNowPoint]); } } }, { deep: true } );
// 监听 marker 变化 watch( () => props.marker, (newMarker) => { if (map) { if (marker) map.remove(marker); if (newMarker) { marker = new AMap.Marker({ position: normalizePoint(newMarker.position), title: newMarker.title || '', }); map.add(marker); } } }, { deep: true } );
// 初始化地图 onMounted(loadMap);
// 卸载地图 onUnmounted(() => { map?.destroy(); }); </script>
<template> <div id="map_container"></div> </template>
<style scoped> #map_container { width: 60%; height: 100%; } </style>
|