Polygon Drawing Example
This example demonstrates how to interactively draw polygons on the map using the DMap3D SDK.
Preview
The user left-clicks to add vertices in sequence, and right-clicks to finish drawing. The polygon is automatically closed, with support for fill and outline styles.
Complete Code
src/components/PolygonDrawing.tsx
import { useEffect, useRef, useState } from 'react'
import * as Cesium from 'cesium'
import DMap3D from 'dmap3d'
import './PolygonDrawing.css'
function PolygonDrawing() {
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,
},
})
// Create polygon drawing tool
const tool = new DMap3D.drawing.polygon({
viewer,
style: {
fillColor: '#0000FF',
fillAlpha: 0.3,
outlineColor: '#FFFFFF',
outlineWidth: 2,
outlineAlpha: 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('Polygon drawing complete, vertex count:', 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)
}
// Load from data
const handleLoadData = () => {
const tool = toolRef.current
if (!tool) return
tool.loadFromData([
[116.39, 39.90],
[116.41, 39.90],
[116.41, 39.92],
[116.39, 39.92],
])
}
return (
<div className="polygon-drawing">
<div ref={containerRef} className="cesium-container" />
<div className="control-panel">
<h3>Polygon Drawing</h3>
<p>Left-click to add vertices, right-click to finish (auto-close)</p>
<div className="buttons">
<button onClick={handleDraw} disabled={isActive}>
{isActive ? 'Drawing...' : 'Start Drawing'}
</button>
<button onClick={handleStop} disabled={!isActive}>Stop</button>
<button onClick={handleClear}>Clear</button>
</div>
<button className="load-btn" onClick={handleLoadData}>
Load Sample Polygon from Data
</button>
</div>
</div>
)
}
export default PolygonDrawing
Stylesheet
src/components/PolygonDrawing.css
.polygon-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; }
Key Code Explanation
1. Create Polygon Drawing Tool
const tool = new DMap3D.drawing.polygon({
viewer,
style: {
fillColor: '#0000FF', // Fill color
fillAlpha: 0.3, // Fill opacity
outlineColor: '#FFFFFF', // Outline color
outlineWidth: 2, // Outline width
outlineAlpha: 1.0, // Outline opacity
},
clampToGround: true, // Clamp to ground
})
2. Interactive Drawing
tool.draw((entity, positions) => {
// entity: Polygon Entity
// positions: Vertex coordinate array
console.log('Vertex count:', positions.length)
})
Left-click to add vertices, right-click to finish drawing and auto-close the polygon.
3. Load from Data
// Longitude-latitude array
tool.loadFromData([
[116.39, 39.90],
[116.41, 39.90],
[116.41, 39.92],
[116.39, 39.92],
])
Extended Features
- Area Calculation - Automatically calculate polygon area
- Vertex Editing - Drag to modify vertices of drawn polygons
- Holes - Cut holes inside polygons
- Boolean Operations - Polygon union, clipping, and intersection