husq
2023-10-08 a0d006a8e6734ce1ff08fc5d196266cc2fd27f7d
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
import { ELocalStorageKey, EDeviceTypeName } from '/@/types'
import * as Cesium from 'cesium'
import {
  getCurrentInstance,
} from 'vue'
interface billboardSetting {
  image: string,
  scale?: number,
  [key: string]: any
}
interface pointSetting {
  pixelSize: number,
  outlineWidth: number,
  [key: string]: any
}
interface labelSetting {
  text: string | undefined,
  font: string,
  outlineWidth: number,
  [key: string]: any
}
interface pointOption {
  longitude: number
  latitude: number
  point?: pointSetting
  billboard?: billboardSetting
  label?: labelSetting
  id?: string
}
// 定义全局的viewer变量防止重复生成
// eslint-disable-next-line no-var
var viewer: Cesium.Viewer | null = null
export function cesiumOperation () {
  let handler: Cesium.ScreenSpaceEventHandler
  const dimension = ref(3)
  const _init = () => {
    // Cesium Token
    const cesiumToken = import.meta.env.VITE_CESIUM_TOKEN
    Cesium.Ion.defaultAccessToken = cesiumToken
    Cesium.Camera.DEFAULT_VIEW_FACTOR = -0.45
    // 西南东北,默认显示中国
    Cesium.Camera.DEFAULT_VIEW_RECTANGLE = Cesium.Rectangle.fromDegrees(110, -25, 110, 90)
    viewer = new Cesium.Viewer('cesiumContainer', {
      infoBox: false, // 禁用沙箱,解决控制台报错
      animation: false, // 左下角的动画仪表盘
      baseLayerPicker: false, // 右上角的图层选择按钮
      geocoder: false, // 搜索框
      homeButton: false, // home按钮
      sceneModePicker: false, // 模式切换按钮
      timeline: false, // 底部的时间轴
      navigationHelpButton: false, // 右上角的帮助按钮,
      selectionIndicator: false, // 是否显示选择指示器
      baseLayer: false,
    })
    handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas)
    viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK) // 禁用双击
    // viewer.scene.screenSpaceCameraController.minimumZoomDistance = 100
    viewer.scene.screenSpaceCameraController.maximumZoomDistance = 4500000
    loadLAYER()
  }
  // 加载图层、注解方法
  const loadLAYER = () => {
    // 天地图Key
    const TDT_Token = 'c6eea7dad4fa1e2d1e32ec0e7c9735db'
    // 天地图地图
    const TDT_IMG_C = 'https://{s}.tianditu.gov.cn/img_c/wmts?service=wmts&request=GetTile&version=1.0.0' +
      '&LAYER=img&tileMatrixSet=c&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}' +
      '&style=default&format=tiles&tk=' + TDT_Token
    // 天地图注记
    const TDT_ZJ = 'https://{s}.tianditu.gov.cn/cia_c/wmts?service=wmts&request=GetTile&version=1.0.0' +
      '&LAYER=cia&tileMatrixSet=c&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}' +
      '&style=default&format=tiles&tk=' + TDT_Token
    // 天地图图层加载
    const imageryProvider = new Cesium.WebMapTileServiceImageryProvider({
      url: TDT_IMG_C,
      layer: 'tdtImg_c',
      style: 'default',
      format: 'tiles',
      tileMatrixSetID: 'c',
      subdomains: ['t0', 't1', 't2', 't3', 't4', 't5', 't6', 't7'],
      tilingScheme: new Cesium.GeographicTilingScheme(),
      tileMatrixLabels: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19'],
      maximumLevel: 50,
    })
    viewer?.imageryLayers.addImageryProvider(imageryProvider)
    // 天地图中文注记加载
    const annotation = new Cesium.WebMapTileServiceImageryProvider({
      url: TDT_ZJ,
      layer: 'tdtImg_c',
      style: 'default',
      format: 'tiles',
      tileMatrixSetID: 'c',
      subdomains: ['t0', 't1', 't2', 't3', 't4', 't5', 't6', 't7'],
      tilingScheme: new Cesium.GeographicTilingScheme(),
      tileMatrixLabels: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19'],
      maximumLevel: 50,
    })
    viewer?.imageryLayers.addImageryProvider(annotation)
  }
 
  // 清除所有标记点
  function removeAllPoint () {
    if (viewer) {
      viewer?.entities?.removeAll()
    }
  }
  // 添加标记点
  const addPoint = (pointOption: pointOption) => {
    if (!pointOption.longitude && !pointOption.latitude) return
    viewer?.entities.add({
      position: Cesium.Cartesian3.fromDegrees(pointOption.longitude, pointOption.latitude),
      billboard: pointOption.billboard,
      point: pointOption.point,
      label: pointOption.label,
      id: pointOption.id,
    })
  }
 
  // 通过点ID删除
  function removeById (id:string) {
    viewer?.entities.removeById(id)
    const pointEntity = viewer?.entities.getById(id)
    if (pointEntity && pointEntity !== undefined) {
      viewer?.entities.remove(pointEntity)
    }
  }
 
  // 飞行 flyto
  const flyTo = (pointOption: pointOption, time: number = 4) => {
    if (!pointOption.longitude && !pointOption.latitude) return
    const destination = Cesium.Cartesian3.fromDegrees(pointOption.longitude, pointOption.latitude, 3000)
    const duration = time
    viewer?.camera.flyTo({
      destination,
      duration
    })
  }
 
  // 二维三维切换
  // 切换为二三维模式
  const switchModel = (type: string) => {
    switch (type) {
      case '2D':
        viewer?.scene.camera.setView({
          orientation: {
            pitch: Cesium.Math.toRadians(-60),
            heading: Cesium.Math.toRadians(0),
          }
        })
        dimension.value = 2
        break
      case '3D':
        viewer?.scene.camera.setView({
          orientation: {
            pitch: Cesium.Math.toRadians(-90),
            heading: Cesium.Math.toRadians(0),
          }
        })
        dimension.value = 3
    }
  }
  onMounted(() => {
    if (viewer) return
    _init()
    const { appContext } = getCurrentInstance()
    const global = appContext.config.globalProperties
    global.$viewer = viewer
  })
 
  // 销毁地图模型
  // onUnmounted(() => {
  //   // viewer = null
  // })
  return {
    addPoint,
    removeAllPoint,
    flyTo,
    removeById,
    switchModel,
    dimension
  }
}