无人机管理后台前端(已迁走)
rain
2025-04-12 4c0ce06dbc18a576f84aa69d67a7dfbe030d0b60
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
<!--
 * @Author: shuishen 1109946754@qq.com
 * @Date: 2024-10-25 15:07:51
 * @LastEditors: shuishen 1109946754@qq.com
 * @LastEditTime: 2025-04-10 13:04:30
 * @FilePath: \drone-web-manage\src\components\map-container\MapContainer.vue
 * @Description: 
 * 
 * Copyright (c) 2024 by shuishen, All Rights Reserved. 
-->
<template>
    <div class="map-container">
        <div class="viewer-container" id="viewer-container">
            <div class="content">
                <slot name="content"></slot>
            </div>
        </div>
    </div>
</template>
 
<script setup>
import { nextTick, onMounted, onUnmounted } from 'vue'
 
window.$viewer = null
window.$Cesium = null
let pointLayer = null
const { VITE_APP_BASE } = import.meta.env
// import * as Cesium from 'cesium'
// import 'cesium/Build/Cesium/Widgets/widgets.css'
const isViewerReady = ref(false)
const { rowDetails } = defineProps({
    rowDetails: {
        type: Object,
        default: () => ({}),
    }
})
 
 
watch(
    [() => isViewerReady.value, () => rowDetails],
    ([ready, detail]) => {
        if (ready && detail?.location) {
            const [lng, lat] = detail.location
            if (!lng || !lat) return
            window.$viewer?.zoomToPosition(new DC.Position(
                lng,
                lat,
                1000,
                0,
                -90,
                0
            ), () => {
            })
 
            let point = new DC.Point(new DC.Position(lng, lat))
            pointLayer.addOverlay(point)
        }
    },
    { deep: true, immediate: true } // 初始化时立即执行
)
 
function initMap () {
    if (window.$viewer) return
 
    nextTick(() => {
        DC.ready({
            // Cesium: Cesium,
            baseUrl: `${VITE_APP_BASE}/libs/dc-sdk/resources/`,
        }).then(() => {
            window.$Cesium = DC.getLib('Cesium')
 
            // 天地图地图
            const imageryProvider_standZh = new window.$Cesium.UrlTemplateImageryProvider({
                url: 'https://t{s}.tianditu.gov.cn/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=e45274b0235bb913eceb393aabbf9c9c',
                subdomains: ['0', '1', '2', '3', '4', '5', '6', '7'],
                maximumLevel: 18,
                credit: 'stand_zj',
            })
            const imageryProvider_stand = new window.$Cesium.UrlTemplateImageryProvider({
                url: 'https://t{s}.tianditu.gov.cn/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=e45274b0235bb913eceb393aabbf9c9c',
                subdomains: ['0', '1', '2', '3', '4', '5', '6', '7'],
                // format: 'image/jpeg',
                // show: true,
                maximumLevel: 18,
                credit: 'stand_tc',
            })
 
            window.$viewer = new DC.Viewer('viewer-container')
            window.$viewer.locationBar.enable = false
 
            window.$viewer?.imageryLayers.addImageryProvider(imageryProvider_stand)
            window.$viewer?.imageryLayers.addImageryProvider(imageryProvider_standZh)
 
            pointLayer = new DC.VectorLayer('pointLayer')
            window.$viewer?.addLayer(pointLayer)
 
            isViewerReady.value = true
        })
    })
}
 
onMounted(() => {
    initMap()
})
 
onUnmounted(() => {
    if (pointLayer) {
        window.$viewer?.removeLayer(pointLayer)
        pointLayer = null
    }
 
    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
    var cesiumContainer = document.getElementById('viewer-container')
    if (cesiumContainer) {
        cesiumContainer.remove() // 移除与地图相关的DOM元素
    }
})
</script>
 
<script>
export default {
    name: 'MapContainer'
}
</script>
 
<style lang="scss" scoped>
.map-container {
    position: relative;
    width: 100% !important;
    height: 100% !important;
    overflow: hidden;
}
 
.viewer-container {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 120%;
    height: 120%;
    transform: translate(-50%, -50%);
}
</style>