zhangxiao
2024-08-20 e47b788ff5f5c699c682999c95da17eb284ca21d
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
<template>
    <div id="amap-container" class="amap-container" :style="getStyle">
        <slot></slot>
    </div>
</template>
<script lang="ts" setup>
import AMapLoader from "@amap/amap-jsapi-loader";
import { MapType } from "./index";
 
const props = withDefaults(
    defineProps<{
        height: number | string;
        options?: AMap.MapOptions;
        mapType?: MapType;
        mapKey: string;
    }>(),
    {
        mapType: "NORMAL_MAP",
        options: () => {
            return {
                viewMode: "3D",
                zoom: 11,
                center: [116.397428, 39.90923]
            };
        }
    }
);
 
const emits = defineEmits<{
    (e: "click", value: any): void;
}>();
 
let map = shallowRef<AMap.Map | null>(null);
 
let infoWindow: AMap.InfoWindow | null = null;
 
provide("map", map);
provide("infoWindow", infoWindow);
 
const getStyle = computed(() => {
    if (typeof props.height === "number") {
        return {
            height: `${props.height}px`
        };
    }
    return {
        height: props.height
    };
});
 
function init() {
    AMapLoader.load({
        key: props.mapKey, //首次load必填
        version: "2.0",
        AMapUI: {
            version: "1.1",
            plugins: ["overlay/SimpleMarker"]
        }
    })
        .then(() => {
            const mapOptions = props.options;
            satellite = new AMap.TileLayer.Satellite();
            map.value = new AMap.Map("amap-container", mapOptions);
            map.value.on("click", function (ev) {
                emits("click", ev);
            });
        })
        .catch((e) => {
            console.error(e);
        });
}
 
let satellite: any = null;
 
watch(
    () => props.mapType,
    (v) => {
        if (map.value) {
            if (v === "NORMAL_MAP") {
                map.value.removeLayer(satellite);
            } else {
                map.value.addLayer(satellite);
            }
        }
    }
);
 
defineExpose({
    map
});
 
onMounted(() => {
    init();
});
 
onUnmounted(() => {
    map.value?.destroy();
});
</script>
<style lang="scss" scoped>
.amap-container {
    padding: 0px;
    margin: 0px;
    width: 100%;
    height: 800px;
}
:deep(.amap-icon) {
    img {
        width: 100%;
    }
}
</style>