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
| <!-- MyMap.vue --> <template> <div :id="state.id"></div> </template> <script setup lang="ts"> import { onMounted, reactive } from "vue"; import AMapLoader from "@amap/amap-jsapi-loader"; const props = defineProps({ modelValue: null, }); const state = reactive({ id: "", map: null as any, }); const initMap = () => { state.id = Math.random().toString(36).substring(7) + Math.floor(Math.random() * 100); AMapLoader.load({ key: "", version: "2.0", plugins: ["AMap.Scale", "AMap.Marker"], }) .then((AMap) => { state.map = new AMap.Map(state.id, { viewMode: "3D", zoom: 18, center: props.modelValue, doubleClickZoom: false, layers: [], }); if (state.map) { const scale = new AMap.Scale({ visible: true, }); state.map.addControl(scale); const marker = new AMap.Marker({ position: props.modelValue, }); state.map.add(marker); } }) .catch((e) => { console.log(e); }); }; onMounted(() => { initMap(); }); </script> <style lang="less" scoped> #container { padding: 0px; margin: 0px; width: 100%; height: 800px; } </style>
|