吉安感知网项目-前端
罗广辉
2026-01-31 a8b8b40b8b849def3a02cd3d655bdfd449d1c44f
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
208
209
210
211
212
213
214
<template>
    <div :id="mapId" class="common-cesium-map" @contextmenu.prevent></div>
</template>
 
<script setup>
import * as Cesium from 'cesium'
 
import { onBeforeUnmount, onMounted, 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: true,
    },
    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)}`
let viewInstance = null
let viewer = null
const ADMIN_BOUNDARY_NAMES = {
    boundary: 'jaBoundarySource',
    line: 'jaBoundaryLineSource',
    label: 'jaBoundaryLabelSource',
}
let adminBoundarySources = null
let initialized = false
let removeCameraListener = null
 
const initMap = async () => {
    if (initialized) return
    initialized = true
    viewInstance = new PublicCesium({
        dom: mapId,
        flatMode: props.flatMode,
        terrain: props.terrain,
        layerMode: props.layerMode,
        boundary: props.boundary,
        contour: props.contour,
    })
    viewer = viewInstance.getViewer()
    if (props.showAdminBoundary) {
        adminBoundarySources = await loadJaAdminBoundary(viewer, {
            zoomTo: props.zoomToBoundary,
        })
    }
    if (props.enableStageEmit) {
        let lastStage = ''
        const onCameraChanged = () => {
            const height = viewer?.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?.camera?.changed?.addEventListener(onCameraChanged)
        removeCameraListener = () => viewer?.camera?.changed?.removeEventListener(onCameraChanged)
    }
    emit('ready', { viewer, publicCesium: viewInstance })
}
 
watch(
    () => props.active,
    active => {
        if (active) initMap()
    }
)
 
onMounted(() => {
    if (props.active) initMap()
})
 
onBeforeUnmount(() => {
    if (removeCameraListener) removeCameraListener()
    removeJaAdminBoundary(viewer, adminBoundarySources)
    adminBoundarySources = null
    viewInstance?.viewerDestroy?.()
})
 
const setAdminBoundaryVisible = async visible => {
    if (!viewer) return
    const removeByNames = () => {
        Object.values(ADMIN_BOUNDARY_NAMES).forEach(name => {
            const sources = viewer.dataSources.getByName(name) || []
            sources.forEach(source => viewer.dataSources.remove(source))
        })
    }
    const resolveSources = () => {
        if (adminBoundarySources) return adminBoundarySources
        const boundarySource = viewer.dataSources.getByName(ADMIN_BOUNDARY_NAMES.boundary)?.[0] ?? null
        const lineSource = viewer.dataSources.getByName(ADMIN_BOUNDARY_NAMES.line)?.[0] ?? null
        const labelSource = viewer.dataSources.getByName(ADMIN_BOUNDARY_NAMES.label)?.[0] ?? null
        if (boundarySource || lineSource || labelSource) {
            adminBoundarySources = { boundarySource, lineSource, labelSource }
        }
        return adminBoundarySources
    }
    const setVisible = (source, next) => {
        if (!source) return
        source.show = next
        source.entities?.values?.forEach(entity => {
            entity.show = next
        })
    }
    const sources = resolveSources()
    if (!visible) {
        if (sources) {
            removeJaAdminBoundary(viewer, sources)
            adminBoundarySources = null
        }
        removeByNames()
        return
    }
    if (sources) {
        const { boundarySource, lineSource, labelSource } = sources
        setVisible(boundarySource, true)
        setVisible(lineSource, true)
        setVisible(labelSource, true)
        return
    }
    adminBoundarySources = await loadJaAdminBoundary(viewer, {
        zoomTo: false,
    })
}
 
const zoomToAdminBoundary = async () => {
    if (!viewer) return
    const resolveSources = () => {
        if (adminBoundarySources) return adminBoundarySources
        const boundarySource = viewer.dataSources.getByName(ADMIN_BOUNDARY_NAMES.boundary)?.[0] ?? null
        const lineSource = viewer.dataSources.getByName(ADMIN_BOUNDARY_NAMES.line)?.[0] ?? null
        const labelSource = viewer.dataSources.getByName(ADMIN_BOUNDARY_NAMES.label)?.[0] ?? null
        if (boundarySource || lineSource || labelSource) {
            adminBoundarySources = { boundarySource, lineSource, labelSource }
        }
        return adminBoundarySources
    }
    let sources = resolveSources()
    if (!sources?.boundarySource) {
        sources = await loadJaAdminBoundary(viewer, { zoomTo: false })
        adminBoundarySources = sources
    }
    const target = sources?.boundarySource
    if (!target) return
    await viewer.flyTo(target, {
        duration: 0,
        offset: new Cesium.HeadingPitchRange(0, Cesium.Math.toRadians(-75), 0),
    })
}
 
const getMap = () => ({ viewer, publicCesium: viewInstance })
const getViewer = () => viewer
const getPublicCesium = () => viewInstance
 
defineExpose({ getMap, getViewer, getPublicCesium, setAdminBoundaryVisible, zoomToAdminBoundary })
</script>
 
<style scoped lang="scss">
.common-cesium-map {
    width: 100%;
    height: 100%;
}
</style>