吉安感知网项目-前端
罗广辉
2026-01-26 beb95fb5fc166804056abafd70fc01ac27de7621
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
<template>
    <div :id="mapId" class="common-cesium-map"></div>
</template>
 
<script setup>
import { onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { PublicCesium } from '@/utils/cesium/publicCesium'
import { loadJaAdminBoundary, removeJaAdminBoundary } from '@/utils/cesium/adminBoundary'
 
const props = defineProps({
    active: {
        type: Boolean,
        default: true,
    },
    domId: {
        type: String,
        default: '',
    },
    flatMode: {
        type: Boolean,
        default: false,
    },
    terrain: {
        type: Boolean,
        default: true,
    },
    layerMode: {
        type: Number,
        default: 4,
    },
    boundary: {
        type: Boolean,
        default: false,
    },
    contour: {
        type: Boolean,
        default: false,
    },
    showAdminBoundary: {
        type: Boolean,
        default: true,
    },
    zoomToBoundary: {
        type: Boolean,
        default: false,
    },
    enableStageEmit: {
        type: Boolean,
        default: false,
    },
    clusterHeight: {
        type: Number,
        default: 100000,
    },
    detailHeight: {
        type: Number,
        default: 10000,
    },
})
 
const emit = defineEmits(['ready', 'height-change', 'stage-change'])
const mapId = props.domId || `common-cesium-map-${Math.random().toString(36).slice(2, 10)}`
const viewInstance = ref(null)
const viewer = ref(null)
let adminBoundarySources = null
let initialized = false
let removeCameraListener = null
 
const initMap = async () => {
    if (initialized) return
    initialized = true
    viewInstance.value = new PublicCesium({
        dom: mapId,
        flatMode: props.flatMode,
        terrain: props.terrain,
        layerMode: props.layerMode,
        boundary: props.boundary,
        contour: props.contour,
    })
    viewer.value = viewInstance.value.getViewer()
    if (props.showAdminBoundary) {
        adminBoundarySources = await loadJaAdminBoundary(viewer.value, {
            zoomTo: props.zoomToBoundary,
        })
    }
    if (props.enableStageEmit) {
        let lastStage = ''
        const onCameraChanged = () => {
            const height = viewer.value?.camera?.positionCartographic?.height
            if (height == null) return
            emit('height-change', height)
            let stage = 'mid'
            if (height >= props.clusterHeight) stage = 'cluster'
            if (height <= props.detailHeight) stage = 'detail'
            if (stage !== lastStage) {
                lastStage = stage
                emit('stage-change', stage)
            }
        }
        viewer.value?.camera?.changed?.addEventListener(onCameraChanged)
        removeCameraListener = () => viewer.value?.camera?.changed?.removeEventListener(onCameraChanged)
    }
    emit('ready', { viewer: viewer.value, publicCesium: viewInstance.value })
}
 
watch(
    () => props.active,
    active => {
        if (active) initMap()
    }
)
 
onMounted(() => {
    if (props.active) initMap()
})
 
onBeforeUnmount(() => {
    if (removeCameraListener) removeCameraListener()
    removeJaAdminBoundary(viewer.value, adminBoundarySources)
    viewInstance.value?.viewerDestroy?.()
})
 
const getMap = () => ({ viewer: viewer.value, publicCesium: viewInstance.value })
const getViewer = () => viewer.value
const getPublicCesium = () => viewInstance.value
const flyBoundary = () => adminBoundarySources.flyBoundary()
 
defineExpose({ getMap, getViewer, getPublicCesium,flyBoundary })
</script>
 
<style scoped lang="scss">
.common-cesium-map {
    width: 100%;
    height: 100%;
}
</style>