guonan
2025-04-14 9e860a560c5a4b81abe2042b8d8698e253730502
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
let removeHandler = null
let htmlLayerCollection = []
 
// 创建撒点
function createPoint(option) {
    const { type, stcd, stnm, imgUrl, text, lng, lat, height, callback, imgSize } = option
    if (!option.lng || !option.lat) {
        return
    }
    let param = {
        type,
        stcd,
        stnm,
        position: Cesium.Cartesian3.fromDegrees(lng, lat, 0),
    }
    if (text) {
        param.label = {
            // 文本。支持显式换行符“ \ n”
            text: option.text || "默认标签",
            // 字体样式,以CSS语法指定字体
            font: "12pt Source Han Sans CN",
            // 字体颜色
            fillColor: Cesium.Color.WHITE,
            // 背景颜色
            backgroundColor: Cesium.Color.BLACK.withAlpha(0.8),
            // 是否显示背景颜色
            showBackground: true,
            // 字体边框
            outline: false,
            // 字体边框颜色
            outlineColor: Cesium.Color.WHITE,
            // 字体边框尺寸
            outlineWidth: 2,
            // 应用于图像的统一比例。比例大于会1.0放大标签,而比例小于会1.0缩小标签。
            scale: 1.0,
            // 设置样式:FILL:填写标签的文本,但不要勾勒轮廓;OUTLINE:概述标签的文本,但不要填写;FILL_AND_OUTLINE:填写并概述标签文本。
            style: Cesium.LabelStyle.FILL_AND_OUTLINE,
            // 相对于坐标的水平位置
            verticalOrigin: Cesium.VerticalOrigin.CENTER,
            // 相对于坐标的水平位置
            horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
            // 该属性指定标签在屏幕空间中距此标签原点的像素偏移量
            // pixelOffset: new Cesium.Cartesian2(-10, -90),
            pixelOffset: new Cesium.Cartesian2(0, -30),
            // 显示在距相机的距离处的属性,多少区间内是可以显示的
            // distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 1500),
            // heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
            disableDepthTestDistance: Number.POSITIVE_INFINITY,
 
            // 是否显示
            show: false,
        }
        param.point = {
            color: Cesium.Color.GREEN,
            pixelSize: 3,
            clampToGround: true,
        }
    }
    // 当创建的点为图片时
    if (imgUrl) {
        param.billboard = {
            // 图像地址,URI或Canvas的属性
            image: imgUrl,
            // 高度(以像素为单位)
            height: imgSize || 20,
            // 宽度(以像素为单位)
            width: imgSize || 20,
            // 逆时针旋转
            // 大小是否以米为单位
            sizeInMeters: false,
            // 相对于坐标的垂直位置
            // verticalOrigin: Cesium.VerticalOrigin.CENTER,
            // 相对于坐标的水平位置
            // horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
            // 该属性指定标签在屏幕空间中距此标签原点的像素偏移量
            // pixelOffset: new Cesium.Cartesian2(0, 3),
            // 应用于图像的统一比例。比例大于会1.0放大标签,而比例小于会1.0缩小标签。
            // scale: 1,
            scaleByDistance: new Cesium.NearFarScalar(1.5e2, 1.0, 1.5e7, 0.5),
            // 显示在距相机的距离处的属性,多少区间内是可以显示的
            distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, option.maxVisibleDistance),
            // 是否显示
            show: true,
            disableDepthTestDistance: Number.POSITIVE_INFINITY,
            // heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
            zIndex: 1,
        }
    }
    const entity = new Cesium.Entity(param)
    let point = viewer.entities.add(entity)
    if (callback) {
        eventMap.set(entity.id, callback)
    }
    pointCollection.push(point)
 
    return point
}
 
// 移除所有点位
function removePoints() {
    if (pointCollection.length > 0) {
        pointCollection.forEach(point => {
            viewer.entities.remove(point)
        })
        pointCollection = []
    }
}
// 隐藏所有点位
function hidePoints() {
    if (pointCollection.length > 0) {
        pointCollection.forEach(point => {
            point.show = false
        })
    }
}
function showPoints() {
    if (pointCollection.length > 0) {
        pointCollection.forEach(point => {
            point.show = false
        })
    }
}
export function hideAllHtmlLayer() {
    if (htmlLayerCollection.length > 0) {
        htmlLayerCollection.forEach(dom => {
            dom.style.display = "none"
        })
    }
}
export function showAllHtmlLayer() {
    if (htmlLayerCollection.length > 0) {
        htmlLayerCollection.forEach(dom => {
            dom.style.display = "block"
        })
    }
}
 
// 创建自定义html图层
export function createHtmlLayer(option) {
    const maxVisibleDistance = option.maxVisibleDistance || 5000
    let point = Cesium.Cartesian3.fromDegrees(option.lng, option.lat, option.height)
    var div = document.createElement("div")
    // div.id = "info_" + option.id
    div.className = "district-count"
    div.onclick = () => {
        if (option.callback) {
            option.callback(option)
        }
    }
    div.innerHTML =
        option.html ||
        `<div style='height: 50px;
    width: 50px;
    position: absolute;
    z-index: 2;
    left: 0;
    top: 0;'>自定义标签<div>`
    document.body.appendChild(div)
    htmlLayerCollection.push(div)
    let scene = viewer.scene
    function showAt(position) {
        if (div) {
            if (position) {
                var windowPosition = new Cesium.Cartesian2()
                // var canvasHeight = scene.canvas.height
                let positions = Cesium.SceneTransforms.worldToWindowCoordinates(
                    scene,
                    position,
                    windowPosition
                )
                // top 方案
                const left = windowPosition.x - 80
                const top = `${windowPosition.y - (div.offsetHeight + 10)}`
 
                if (left > 0 && top > 0) {
                    div.style.position = "absolute"
                    div.style.left = `${left}px`
                    div.style.top = `${top}px`
                } else {
                    div.style.display = "none"
                }
            }
        }
    }
 
    function setVisible(visible) {
        div.style.display = visible ? "block" : "none"
    }
    removeHandler = viewer.scene.postRender.addEventListener(e => {
        // let isVisible = new Cesium.EllipsoidalOccluder(
        //     Cesium.Ellipsoid.WGS84,
        //     viewer.camera.position
        // ).isPointVisible(point)
        if (isOverDistance(maxVisibleDistance)) {
            setVisible(false)
        } else {
            setVisible(true)
            showAt(point)
        }
    })
    return div
}
 
function removeAllHtmlLayer() {
    if (htmlLayerCollection.length > 0) {
        htmlLayerCollection.forEach(dom => {
            document.body.removeChild(dom)
            // let dom = document.getElementById(id)
            // if (dom) {
            //   document.body.removeChild(dom)
            // }
        })
        htmlLayerCollection = []
    }
}
 
export function isVisibleDistance(miniVisibleDistance = 0, maxVisibleDistance = 5000000) {
    const height = viewer.camera.positionCartographic.height
    if (height >= miniVisibleDistance && height <= maxVisibleDistance) {
        return true
    }
    return false
}