跳到主要内容

线绘制示例

这个示例演示如何使用 DMap3D SDK 在地图上交互式绘制折线。

效果预览

用户左键依次点击添加折线顶点,右键完成绘制,支持贴地模式。

完整代码

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

function LineDrawing() {
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,
terrain: Cesium.Terrain.fromWorldTerrain(),
})
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.drawing.polyline({
viewer,
style: {
width: 3,
color: '#FF0000',
alpha: 1.0,
},
clampToGround: true,
})
toolRef.current = tool

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

const handleDraw = () => {
const tool = toolRef.current
if (!tool) return
tool.draw((entity: any, positions: Cesium.Cartesian3[]) => {
console.log('线绘制完成,顶点数:', positions.length)
setIsActive(false)
})
setIsActive(true)
}

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

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

// 从数据加载
const handleLoadData = () => {
const tool = toolRef.current
if (!tool) return
tool.loadFromData([
[116.39, 39.90, 0],
[116.40, 39.91, 0],
[116.41, 39.90, 0],
[116.42, 39.91, 0],
])
}

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

<div className="control-panel">
<h3>线绘制</h3>
<p>左键添加顶点,右键完成绘制</p>

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

<button className="load-btn" onClick={handleLoadData}>
从数据加载示例线
</button>
</div>
</div>
)
}

export default LineDrawing

样式文件

src/components/LineDrawing.css
.line-drawing {
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; }

.load-btn {
width: 100%; padding: 8px; border: 1px solid #1890ff; border-radius: 4px;
background: white; color: #1890ff; cursor: pointer; font-size: 14px;
}
.load-btn:hover { background: #e6f7ff; }

关键代码说明

1. 创建线绘制工具

const tool = new DMap3D.drawing.polyline({
viewer,
style: {
width: 3, // 线宽(像素)
color: '#FF0000', // 线颜色
alpha: 1.0, // 透明度
},
clampToGround: true, // 贴地
})

2. 从数据加载

// 经纬度数组格式
tool.loadFromData([
[116.39, 39.90, 0],
[116.40, 39.91, 0],
[116.41, 39.90, 0],
])

// Cartesian3 格式
tool.loadFromData([
Cesium.Cartesian3.fromDegrees(116.39, 39.90),
Cesium.Cartesian3.fromDegrees(116.40, 39.91),
])

3. 动态更新样式

tool.setStyle({
width: 5,
color: '#00FF00',
alpha: 0.8,
})

扩展功能

  1. 虚线样式 - 支持虚线和箭头线型
  2. 距离标注 - 在线段上标注长度
  3. 顶点编辑 - 拖拽修改折线顶点
  4. 路径动画 - 沿线路径播放动画

相关 API