智慧园区前端大屏
shuishen
2025-09-15 17a81d641608545682e3f79f7c6090cac48fe81a
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
<!--
 * @Author: shuishen 1109946754@qq.com
 * @Date: 2024-10-25 15:07:51
 * @LastEditors: shuishen 1109946754@qq.com
 * @LastEditTime: 2025-08-06 00:13:28
 * @FilePath: \jsProject\bigScreen\src\components\global\MapContainer.vue
 * @Description: 
 * 
 * Copyright (c) 2024 by shuishen, All Rights Reserved. 
-->
<template>
    <div class="viewer-container" id="viewer-container">
        <div v-show="isShowRim" class="rim-box" :style="{
            top: currentTop,
            left: currentLeft
        }" @click="rimClick">周边检索</div>
 
        <div class="content">
            <slot name="content"></slot>
        </div>
    </div>
</template>
 
<script setup>
import * as turf from '@turf/turf'
import { useMap } from 'store/map'
import { ref, nextTick, onMounted, onUnmounted, onBeforeUnmount } from 'vue'
import { useRouter, useRoute } from 'vue-router'
let router = useRouter()
window.$viewer = null
window.$Cesium = null
window.$turf = null
const { VITE_APP_BASE } = import.meta.env
// import * as Cesium from 'cesium'
// import 'cesium/Build/Cesium/Widgets/widgets.css'
 
const store = useMap()
 
const isShowRim = ref(false)
const currentTop = ref('0px')
const currentLeft = ref('0px')
 
const currentPosition = ref(null)
 
const viewerRightClick = (e) => {
    currentLeft.value = e.windowPosition.x + 'px'
    currentTop.value = e.windowPosition.y + 'px'
 
    currentPosition.value = e.wgs84Position ? e.wgs84Position : e.wgs84SurfacePosition
 
    isShowRim.value = true
}
 
const viewerClick = (e) => {
    isShowRim.value = false
}
 
const rimClick = () => {
    isShowRim.value = false
 
    store.setRimPosition(currentPosition.value)
 
    router.push({
        path: '/layout/map/main/rim'
    })
}
 
function initMap () {
    if (window.$viewer) return
 
    nextTick(() => {
        DC.ready({
            // Cesium: Cesium,
            baseUrl: `${VITE_APP_BASE}libs/dc-sdk/resources/`,
            turf,
        }).then(() => {
            window.$Cesium = DC.getLib('Cesium')
            window.$turf = DC.getLib('turf')
 
            window.$viewer = new DC.Viewer('viewer-container')
            // window.$viewer.locationBar.enable = true
 
            window.$viewer.zoomToPosition(new DC.Position(
                115.6080,
                29.7880,
                3000,
                0,
                -45,
                0
            ), () => {
                window.$viewer.on(DC.MouseEventType.CLICK, viewerClick)
                window.$viewer.on(DC.MouseEventType.RIGHT_CLICK, viewerRightClick)
 
                store.setLoadMap(true)
            })
        })
    })
}
 
onMounted(() => {
    initMap()
})
 
onBeforeUnmount(() => {
    window.$viewer && window.$viewer.off(DC.MouseEventType.CLICK, viewerClick)
    window.$viewer && window.$viewer.off(DC.MouseEventType.RIGHT_CLICK, viewerRightClick)
})
 
onUnmounted(() => {
    window.$viewer?.entities.removeAll()
    window.$viewer?.imageryLayers.removeAll()
    window.$viewer?.dataSources.removeAll()
    let gl = window.$viewer.scene.context._originalGLContext
    gl.canvas.width = 1
    gl.canvas.height = 1
 
    window.$viewer && window.$viewer.setTerrain()
    window.$viewer && window.$viewer.destroy()
    window.$viewer = null
    delete window.$viewer
    window.$Cesium = null
    delete window.$Cesium
    window.$turf = null
    delete window.$turf
    var cesiumContainer = document.getElementById('viewer-container')
    if (cesiumContainer) {
        cesiumContainer.remove() // 移除与地图相关的DOM元素
    }
    store.setLoadMap(false)
})
</script>
 
<script>
export default {
    name: 'MapContainer'
}
</script>
 
<style lang="scss" scoped>
.viewer-container {
    position: absolute;
    width: 100%;
    height: 100%;
 
    .rim-box {
        padding: 6px 8px;
        position: absolute;
        top: 0;
        left: 0;
        color: #fff;
        background: rgba(28, 115, 195, 0.5);
        border-radius: 5px;
        cursor: pointer;
        z-index: 999;
        transform: translate(10px, -50%);
        box-sizing: border-box;
    }
}
</style>