智慧园区前端大屏
shuishen
2024-12-18 9a37c5e1b2190c3f6ec71483923922189091dd52
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
<!--
 * @Author: shuishen 1109946754@qq.com
 * @Date: 2024-10-25 15:07:51
 * @LastEditors: shuishen 1109946754@qq.com
 * @LastEditTime: 2024-12-03 12:21:28
 * @FilePath: \bigScreen\src\components\global\MapContainer.vue
 * @Description: 
 * 
 * Copyright (c) 2024 by shuishen, All Rights Reserved. 
-->
<template>
  <div class="viewer-container" id="viewer-container">
    <div class="content">
      <slot name="content"></slot>
    </div>
  </div>
</template>
 
<script setup>
import { useMap } from 'store/map'
import { nextTick, onMounted, onUnmounted } from 'vue'
import * as turf from '@turf/turf'
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()
 
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.1021,
        27.2360,
        5000,
        0,
        -45,
        0
      ), () => {
        store.setLoadMap(true)
      })
    })
  })
}
 
onMounted(() => {
  initMap()
})
 
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%;
}
</style>