跳到主要内容

高度测量

DMap3D.measurement.height 用于测量三维空间中两点之间的垂直高度差。

引入

import DMap3D from 'dmap3d'
import * as Cesium from 'cesium'

基本用法

const viewer = new Cesium.Viewer('cesiumContainer')

// 创建高度测量工具
const heightTool = new DMap3D.measurement.height(viewer)

// 监听测量完成事件
heightTool.onMeasureEnd((result) => {
console.log('绝对高度:', result.absoluteHeight.toFixed(2), '米')
console.log('相对高度:', result.relativeHeight.toFixed(2), '米')
console.log('水平距离:', result.horizontalDistance.toFixed(2), '米')
})

// 激活测量(左键点击两次完成)
heightTool.activate()

构造函数

new DMap3D.measurement.height(viewer, options?)

创建高度测量工具实例。

参数:

  • viewer - Cesium.Viewer 实例
  • options? - 可选配置项

Options:

interface HeightOptions {
colors?: {
line?: string // 垂直线颜色,默认 '#FBFB00'
point?: string // 测量点颜色,默认 '#0008FF'
circular?: string // 高度标记圆颜色,默认 '#33fc05'
}
fontSize?: number // 字体大小,默认 14
}

示例:

const heightTool = new DMap3D.measurement.height(viewer, {
colors: {
line: '#00FFFF',
point: '#FF00FF',
circular: '#FFFF00'
},
fontSize: 16
})

方法

activate()

激活高度测量。用户在场景中左键点击第一个点(起点),再点击第二个点(终点)完成测量。

heightTool.activate()

deactivate()

停用测量,但保留已绘制的测量结果。

heightTool.deactivate()

clear()

清除所有测量结果(点、垂直线、高度标记圆和标注)。

heightTool.clear()

destroy()

销毁工具实例并清理资源。

heightTool.destroy()

onMeasureEnd(callback)

注册测量完成回调函数。

heightTool.onMeasureEnd((result) => {
console.log('绝对高度:', result.absoluteHeight, '米')
console.log('相对高度:', result.relativeHeight, '米')
console.log('水平距离:', result.horizontalDistance, '米')
})

getResult()

获取当前测量结果,测量未完成时返回 null

const result = heightTool.getResult()
if (result) {
console.log('相对高度:', result.relativeHeight.toFixed(2), '米')
}

测量结果

HeightResult 对象:

interface HeightResult {
// 终点的绝对高度(米)
absoluteHeight: number

// 两点之间的相对高度差(米,终点高度 - 起点高度)
relativeHeight: number

// 两点之间的水平距离(米)
horizontalDistance: number
}

完整示例

const viewer = new Cesium.Viewer('cesiumContainer')

// 创建高度测量工具
const heightTool = new DMap3D.measurement.height(viewer, {
colors: {
line: '#00FFFF',
point: '#FFFF00',
circular: '#00FF00'
},
fontSize: 16
})

// 监听测量完成事件
heightTool.onMeasureEnd((result) => {
const { absoluteHeight, relativeHeight, horizontalDistance } = result
console.log(`绝对高度: ${absoluteHeight.toFixed(2)}`)
console.log(`相对高度差: ${relativeHeight.toFixed(2)}`)
console.log(`水平距离: ${horizontalDistance.toFixed(2)}`)
})

// 激活测量
heightTool.activate()

相关链接