跳到主要内容

角度测量示例

这个示例演示如何使用 DMap3D SDK 进行三点角度测量。

效果预览

用户依次点击三个点(起点、顶点、终点),自动计算并显示顶点处的夹角,同时绘制扇形区域。

完整代码

src/components/AngleMeasurement.tsx
import { useEffect, useRef, useState } from 'react'
import * as Cesium from 'cesium'
import DMap3D from 'dmap3d'
import './AngleMeasurement.css'

function AngleMeasurement() {
const containerRef = useRef<HTMLDivElement>(null)
const viewerRef = useRef<Cesium.Viewer | null>(null)
const toolRef = useRef<any>(null)
const [isActive, setIsActive] = useState(false)

useEffect(() => {
if (!containerRef.current) return

Cesium.Ion.defaultAccessToken = 'your-cesium-ion-token'

const viewer = new Cesium.Viewer(containerRef.current, {
animation: false,
fullscreenButton: false,
geocoder: false,
homeButton: false,
sceneModePicker: false,
selectionIndicator: false,
timeline: false,
navigationHelpButton: false,
infoBox: false,
baseLayerPicker: false,
terrainProvider: new Cesium.EllipsoidTerrainProvider({}),
})
viewerRef.current = viewer

viewer.camera.setView({
destination: Cesium.Cartesian3.fromDegrees(116.4074, 39.9042, 5000),
orientation: {
heading: Cesium.Math.toRadians(0),
pitch: Cesium.Math.toRadians(-45),
roll: 0,
},
})

// 创建角度测量工具
const tool = new DMap3D.measurement.angle(viewer)
tool.updateConfig({
pointColor: '#ff0000',
pointSize: 8,
lineColor: '#ffff00',
lineWidth: 2,
fontColor: '#ffffff',
fontSize: 16,
sectorColor: 'rgba(255, 255, 0, 0.3)',
sectorLineColor: '#ffff00',
})
toolRef.current = tool

return () => {
tool.destroy()
viewer.destroy()
}
}, [])

const handleStart = () => {
const tool = toolRef.current
if (!tool) return
tool.start()
setIsActive(true)
}

const handleStop = () => {
const tool = toolRef.current
if (!tool) return
tool.end()
setIsActive(false)
}

const handleClear = () => {
const tool = toolRef.current
if (!tool) return
if (isActive) tool.end()
tool.clear()
setIsActive(false)
}

return (
<div className="angle-measurement">
<div ref={containerRef} className="cesium-container" />

<div className="control-panel">
<h3>角度测量</h3>
<p>依次点击三个点:起点、顶点、终点</p>

<div className="buttons">
<button onClick={handleStart} disabled={isActive}>
{isActive ? '测量中...' : '开始测量'}
</button>
<button onClick={handleStop} disabled={!isActive}>停止</button>
<button onClick={handleClear}>清除</button>
</div>

<div className="tip">
<h4>操作说明</h4>
<ol>
<li>点击第一个点(起始边端点)</li>
<li>点击第二个点(角的顶点)</li>
<li>点击第三个点(终止边端点)</li>
<li>自动显示角度值和扇形区域</li>
</ol>
</div>
</div>
</div>
)
}

export default AngleMeasurement

样式文件

src/components/AngleMeasurement.css
.angle-measurement {
position: relative;
width: 100%;
height: 100vh;
}

.cesium-container {
width: 100%;
height: 100%;
}

.control-panel {
position: absolute;
top: 20px;
right: 20px;
background: rgba(255, 255, 255, 0.95);
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
min-width: 250px;
}

.control-panel h3 { margin: 0 0 10px 0; font-size: 18px; }
.control-panel p { margin: 0 0 15px 0; color: #666; font-size: 14px; }
.buttons { display: flex; gap: 8px; margin-bottom: 15px; }
.buttons button {
flex: 1; padding: 8px 12px; border: none; border-radius: 4px;
background: #1890ff; color: white; cursor: pointer; font-size: 14px;
}
.buttons button:hover { background: #40a9ff; }
.buttons button:disabled { background: #d9d9d9; cursor: not-allowed; }
.tip { border-top: 1px solid #e8e8e8; padding-top: 15px; }
.tip h4 { margin: 0 0 8px 0; font-size: 14px; color: #666; }
.tip ol { padding-left: 20px; margin: 0; }
.tip li { font-size: 13px; margin: 4px 0; color: #666; }

关键代码说明

1. 创建角度测量工具

const tool = new DMap3D.measurement.angle(viewer)
tool.updateConfig({
pointColor: '#ff0000', // 测量点颜色
sectorColor: 'rgba(255, 255, 0, 0.3)', // 扇形填充色
sectorLineColor: '#ffff00', // 扇形边线颜色
})

2. 测量控制

tool.start()   // 开始测量
tool.end() // 结束测量
tool.clear() // 清除结果
tool.destroy() // 销毁工具

3. 更新配置

tool.updateConfig({
pointColor: '#ff0000',
pointSize: 8,
lineColor: '#ffff00',
lineWidth: 2,
fontColor: '#ffffff',
fontSize: 16,
})

扩展功能

  1. 角度单位切换 - 支持度数、弧度、百分度
  2. 多角度测量 - 连续测量多个角度
  3. 角平分线 - 自动绘制角平分线
  4. 数据导出 - 导出角度测量结果

相关 API