Skip to main content

Area Measurement Example

This example demonstrates how to perform area measurement using the DMap3D SDK.

Preview

The user can click on the map to draw a polygon, double-click to finish drawing, and display the area.

Complete Code

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

interface MeasurementResult {
area: number
perimeter: number
}

function AreaMeasurement() {
const containerRef = useRef<HTMLDivElement>(null)
const viewerRef = useRef<Cesium.Viewer | null>(null)
const toolRef = useRef<any>(null)
const [result, setResult] = useState<MeasurementResult | null>(null)
const [isMeasuring, setIsMeasuring] = useState(false)

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

// Create Viewer
const viewer = new Cesium.Viewer(containerRef.current, {
terrain: Cesium.Terrain.fromWorldTerrain(),
timeline: false,
animation: false,
baseLayerPicker: false,
})
viewerRef.current = viewer

// Fly to Beijing
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(116.3912757, 39.906217, 10000),
})

// Create measurement tool
const tool = new DMap3D.measurement.area(viewer, {
fillColor: Cesium.Color.BLUE.withAlpha(0.3),
outlineColor: Cesium.Color.BLUE,
outlineWidth: 3,
showLabel: true,
clampToGround: true,
})
toolRef.current = tool

// Listen for measurement start
tool.on('measureStart', () => {
setIsMeasuring(true)
setResult(null)
})

// Listen for measurement update
tool.on('measureUpdate', (data: MeasurementResult) => {
setResult(data)
})

// Listen for measurement completion
tool.on('measureComplete', (data: MeasurementResult) => {
setIsMeasuring(false)
setResult(data)
})

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

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

const handleClear = () => {
toolRef.current?.clear()
setResult(null)
setIsMeasuring(false)
}

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

{/* Control Panel */}
<div className="control-panel">
<h3>Area Measurement</h3>
<p>Click on the map to draw a polygon, double-click to finish</p>

<div className="buttons">
<button onClick={handleStart} disabled={isMeasuring}>
{isMeasuring ? 'Measuring...' : 'Start Measurement'}
</button>
<button onClick={handleClear}>Clear</button>
</div>

{/* Display results */}
{result && (
<div className="result">
<h4>Measurement Result</h4>
<p>Area: {result.area.toFixed(2)} sq m</p>
<p>Area: {(result.area / 10000).toFixed(4)} hectares</p>
<p>Perimeter: {result.perimeter.toFixed(2)} m</p>
</div>
)}
</div>
</div>
)
}

export default AreaMeasurement

Stylesheet

src/components/AreaMeasurement.css
.area-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: 10px;
margin-bottom: 15px;
}

.buttons button {
flex: 1;
padding: 8px 16px;
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;
}

.result {
border-top: 1px solid #e8e8e8;
padding-top: 15px;
}

.result h4 {
margin: 0 0 10px 0;
font-size: 16px;
}

.result p {
margin: 5px 0;
font-size: 14px;
color: #333;
}

Key Code Explanation

1. Create Viewer

const viewer = new Cesium.Viewer(containerRef.current, {
terrain: Cesium.Terrain.fromWorldTerrain(), // Use world terrain
timeline: false, // Hide timeline
animation: false, // Hide animation widget
baseLayerPicker: false, // Hide base layer picker
})

2. Create Measurement Tool

const tool = new DMap3D.measurement.area(viewer, {
fillColor: Cesium.Color.BLUE.withAlpha(0.3), // Semi-transparent blue fill
outlineColor: Cesium.Color.BLUE, // Blue outline
outlineWidth: 3, // Outline width
showLabel: true, // Show label
clampToGround: true, // Clamp to ground
})

3. Listen for Events

// Measurement start
tool.on('measureStart', () => {
setIsMeasuring(true)
})

// Measurement update (real-time display)
tool.on('measureUpdate', (data) => {
setResult(data)
})

// Measurement complete
tool.on('measureComplete', (data) => {
setIsMeasuring(false)
setResult(data)
})

Extended Features

You can add the following features based on this example:

  1. Unit Switching - Support square meters, hectares, square kilometers, and other units
  2. Data Export - Export measurement results as JSON or CSV
  3. History - Save multiple measurement results
  4. Style Customization - Let users customize colors and styles