跳到主要内容

方位角测量示例

这个示例演示如何使用 DMap3D SDK 进行两点方位角测量。

效果预览

用户点击两个点,自动计算从第一个点到第二个点的方位角(以正北为基准,顺时针方向 0°-360°),并显示北向指示箭头。

完整代码

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

function AzimuthMeasurement() {
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.azimuth(viewer)
tool.updateConfig({
pointColor: '#ff0000',
lineColor: '#00ffff',
fontColor: '#ffffff',
fontSize: 16,
arrowColor: '#ff0000',
arrowLength: 50,
})
toolRef.current = tool

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

const handleStart = () => {
toolRef.current?.start()
setIsActive(true)
}

const handleStop = () => {
toolRef.current?.end()
setIsActive(false)
}

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

return (
<div className="azimuth-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="compass">
<h4>方位角参考</h4>
<ul>
<li><strong>0° / 360°</strong> - 正北</li>
<li><strong>90°</strong> - 正东</li>
<li><strong>180°</strong> - 正南</li>
<li><strong>270°</strong> - 正西</li>
</ul>
</div>
</div>
</div>
)
}

export default AzimuthMeasurement

样式文件

src/components/AzimuthMeasurement.css
.azimuth-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; }

.compass { border-top: 1px solid #e8e8e8; padding-top: 15px; }
.compass h4 { margin: 0 0 8px 0; font-size: 14px; color: #666; }
.compass ul { list-style: none; padding: 0; margin: 0; }
.compass li { font-size: 13px; margin: 4px 0; color: #666; }

关键代码说明

1. 创建方位角测量工具

const tool = new DMap3D.measurement.azimuth(viewer)
tool.updateConfig({
arrowColor: '#ff0000', // 北向指示箭头颜色
arrowLength: 50, // 箭头长度
})

2. 方位角说明

方位角以正北方向为基准,顺时针旋转度量:

方位角方向
正北
45°东北
90°正东
135°东南
180°正南
225°西南
270°正西
315°西北

扩展功能

  1. 磁偏角修正 - 考虑真北和磁北的偏差
  2. 反方位角 - 同时显示正反方位角
  3. 导航线 - 绘制方位角导航辅助线
  4. 坐标方位角 - 批量计算坐标串的方位角

相关 API