胡思旗
2023-08-29 b80ee8c883f279e843071e6b53ab9cddec3f88a0
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
<!--
 * @Author: 胡思旗 931347610@qq.com
 * @Date: 2023-08-22 17:50:30
 * @LastEditors: 胡思旗 931347610@qq.com
 * @LastEditTime: 2023-08-25 10:06:16
 * @FilePath: \Cloud-API-Demo-Web\src\components\cesiumMap\cesium.vue
 * @Description:
 *
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved.
-->
<template>
      <div class="height-100 width-100 cesium" id="cesiumContainer" @click="getCoordinate"></div>
</template>
 
<script setup lang="ts">
import * as Cesium from 'cesium'
import { onMounted, ref, onUnmounted } from 'vue'
const viewer: { value: Cesium.Viewer | null | undefined } = ref()
const init = () => {
  // Cesium Token
  const cesiumToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJhMjdmNGUxZC02YzY3LTQyZWUtOTNmYy1hNTI0MDRkZDY2ZmEiLCJpZCI6MTYxODgyLCJpYXQiOjE2OTI3NTQyNDV9.Pm7xTwPmKowPzFgJ0TsIKOtthigq86BLJX4c8M97Hhw'
  Cesium.Ion.defaultAccessToken = cesiumToken
  Cesium.Camera.DEFAULT_VIEW_FACTOR = -0.45
  // 西南东北,默认显示中国
  Cesium.Camera.DEFAULT_VIEW_RECTANGLE = Cesium.Rectangle.fromDegrees(110, -25, 110, 90)
  viewer.value = new Cesium.Viewer('cesiumContainer', {
    infoBox: false, // 禁用沙箱,解决控制台报错
    animation: false, // 左下角的动画仪表盘
    baseLayerPicker: false, // 右上角的图层选择按钮
    geocoder: false, // 搜索框
    homeButton: false, // home按钮
    sceneModePicker: false, // 模式切换按钮
    timeline: false, // 底部的时间轴
    navigationHelpButton: false, // 右上角的帮助按钮,
    baseLayer: false,
  })
  loadLAYER()
}
// 加载图层、注解方法
const loadLAYER = () => {
  // 天地图Key
  const TDT_Token = 'c6eea7dad4fa1e2d1e32ec0e7c9735db'
  // 天地图地图
  const TDT_IMG_C = 'http://{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 = 'http://{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.value?.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.value?.imageryLayers.addImageryProvider(annotation)
}
// 相机设置
const camera = () => {}
// 获取地图点击坐标
const getCoordinate = () => {
  let longitude: number, latitude: number
  viewer.value?.screenSpaceEventHandler.setInputAction((e: any) => {
    const cartesian = viewer.value?.camera.pickEllipsoid(e.position, viewer.value?.scene.globe.ellipsoid)
    if (cartesian) {
      const cartographic = Cesium.Cartographic.fromCartesian(cartesian)
      longitude = Cesium.Math.toDegrees(cartographic.longitude)
      latitude = Cesium.Math.toDegrees(cartographic.latitude)
    }
    Point(longitude, latitude)
    return { longitude, latitude }
  }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
}
 
const Point = (longitude:number, latitude:number) => {
  const entity = viewer.value?.entities.add({
    position: Cesium.Cartesian3.fromDegrees(longitude, latitude),
    point: {
      pixelSize: 10,
      color: Cesium.Color.YELLOW
    }
  })
  // // 将相机定位到标记点的位置
  // viewer.value?.camera.flyTo({
  //   destination: entity.position,
  //   duration: 2
  // })
}
 
onMounted(() => {
  init()
})
// 销毁地图模型
onUnmounted(() => {
  viewer.value = null
})
 
</script>
 
<style scoped lang="scss">
.cesium{
  :deep(.cesium-viewer-bottom){
    display: none !important;
  }
}
 
</style>