吉安感知网项目-前端
shuishen
2026-01-28 046a69a1a3b50079eae3663cd631bd062748ec4f
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
import * as Cesium from 'cesium'
import { MapTooltip } from '../Tooltip'
import { ToolBase } from '../ToolBase'
import { getBoundsFromCartesians, rectanglePositionsFromBounds } from '../shapeUtils'
 
export class DrawRectangleTool extends ToolBase {
    constructor(viewer) {
        super(viewer)
        this.tooltip = new MapTooltip(viewer)
        this.dataSource = null
        this.rectangleEntity = null
        this.outlineEntity = null
        this.startPosition = null
        this.endPosition = null
        this.isDrawing = false
    }
 
    start() {
        this.dataSource = new Cesium.CustomDataSource('draw-rectangle')
        this.viewer.dataSources.add(this.dataSource)
        this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas)
 
        this.handler.setInputAction(click => this.handleLeftClick(click), Cesium.ScreenSpaceEventType.LEFT_CLICK)
        this.handler.setInputAction(movement => this.handleMouseMove(movement), Cesium.ScreenSpaceEventType.MOUSE_MOVE)
 
        this.tooltip.show('单击设置起点', { x: 0, y: 0 })
    }
 
    handleLeftClick(click) {
        const position = this.getPositionFromScreen(click.position)
        if (!position) return
 
        if (!this.startPosition) {
            this.startPosition = position
            this.endPosition = position
            this.isDrawing = true
            this.createEntities()
            this.tooltip.show('单击结束绘制', click.position)
            return
        }
 
        if (this.isDrawing) {
            this.endPosition = position
            this.isDrawing = false
            const positions = this.getRectanglePositions()
            this.notify('getPolygonPositions', positions)
            this.tooltip.hide()
            this.clearPreviewEntities()
        }
    }
 
    handleMouseMove(movement) {
        if (!this.isDrawing) {
            this.tooltip.move(movement.endPosition)
            return
        }
        const position = this.getPositionFromScreen(movement.endPosition)
        if (!position) return
        this.endPosition = position
        this.tooltip.move(movement.endPosition)
    }
 
    createEntities() {
        if (this.rectangleEntity) return
        const hierarchy = new Cesium.CallbackProperty(() => {
            const positions = this.getRectanglePositions()
            return new Cesium.PolygonHierarchy(positions)
        }, false)
 
        this.rectangleEntity = this.dataSource.entities.add({
            polygon: {
                hierarchy,
                material: Cesium.Color.fromBytes(45, 140, 240, 99),
                outline: false,
                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
            },
        })
 
        this.outlineEntity = this.dataSource.entities.add({
            polyline: {
                positions: new Cesium.CallbackProperty(() => {
                    const positions = this.getRectanglePositions()
                    return positions.length ? [...positions, positions[0]] : []
                }, false),
                clampToGround: true,
                width: 2,
                material: Cesium.Color.fromBytes(45, 140, 240, 255),
            },
        })
    }
 
    getRectanglePositions() {
        if (!this.startPosition || !this.endPosition) return []
        const bounds = getBoundsFromCartesians([this.startPosition, this.endPosition])
        return rectanglePositionsFromBounds(bounds)
    }
 
    clearPreviewEntities() {
        if (!this.dataSource) return
        this.dataSource.entities.removeAll()
        this.rectangleEntity = null
        this.outlineEntity = null
    }
 
    getPositionFromScreen(screenPosition) {
        const scene = this.viewer.scene
        const cartesian = scene.pickPosition(screenPosition)
        if (cartesian) return cartesian
        return scene.camera.pickEllipsoid(screenPosition, scene.globe.ellipsoid)
    }
 
    destroy() {
        if (this.dataSource) {
            this.dataSource.entities.removeAll()
            this.viewer.dataSources.remove(this.dataSource)
            this.dataSource = null
        }
        if (this.handler) {
            this.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
            this.handler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE)
            this.handler.destroy()
            this.handler = null
        }
        this.tooltip?.destroy()
        this.tooltip = null
        this.startPosition = null
        this.endPosition = null
        this.isDrawing = false
    }
}