Skip to main content

Point Drawing Example

This example demonstrates how to interactively draw point markers on the map using the DMap3D SDK.

Preview

The user clicks on the map to add point markers, with support for custom styles, clearing, and loading from data.

Complete Code

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

function PointDrawing() {
const containerRef = useRef<HTMLDivElement>(null)
const viewerRef = useRef<Cesium.Viewer | null>(null)
const toolRef = useRef<any>(null)
const [isActive, setIsActive] = useState(false)
const [pointCount, setPointCount] = useState(0)

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 point drawing tool
const tool = new DMap3D.drawing.point({
viewer,
style: {
radius: 10,
fillColor: '#FF0000',
strokeColor: '#FFFFFF',
fillAlpha: 1.0,
strokeAlpha: 1.0,
width: 2,
},
})
toolRef.current = tool

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

const handleDraw = () => {
const tool = toolRef.current
if (!tool) return
tool.draw((entity: any, position: Cesium.Cartesian3) => {
setPointCount((prev) => prev + 1)
const carto = Cesium.Cartographic.fromCartesian(position)
console.log(
'Point:',
Cesium.Math.toDegrees(carto.longitude).toFixed(6),
Cesium.Math.toDegrees(carto.latitude).toFixed(6)
)
})
setIsActive(true)
}

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

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

// Load sample points from data
const handleLoadData = () => {
const tool = toolRef.current
if (!tool) return
tool.loadFromData(
Cesium.Cartesian3.fromDegrees(116.4074, 39.9042, 0)
)
setPointCount((prev) => prev + 1)
}

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

<div className="control-panel">
<h3>Point Drawing</h3>
<p>Click on the map to add point markers</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 Point from Data
</button>

<div className="result">
<p>Points drawn: {pointCount}</p>
</div>
</div>
</div>
)
}

export default PointDrawing

Stylesheet

src/components/PointDrawing.css
.point-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;
margin-bottom: 15px;
}
.load-btn:hover { background: #e6f7ff; }

.result { border-top: 1px solid #e8e8e8; padding-top: 10px; }
.result p { margin: 0; font-size: 14px; color: #333; }

Key Code Explanation

1. Create Point Drawing Tool

const tool = new DMap3D.drawing.point({
viewer,
style: {
radius: 10, // Point radius
fillColor: '#FF0000', // Fill color
strokeColor: '#FFFFFF', // Stroke color
fillAlpha: 1.0, // Fill opacity
width: 2, // Stroke width
},
})

2. Interactive Drawing

tool.draw((entity, position) => {
// entity: Cesium Entity instance
// position: Cesium.Cartesian3 coordinate
})

3. Load from Data

tool.loadFromData(Cesium.Cartesian3.fromDegrees(116.4074, 39.9042, 0))

Extended Features

  1. Custom Icons - Use Billboard instead of default point style
  2. Drag Edit - Support dragging to move drawn points
  3. Property Popup - Show property info popup when clicking a point marker
  4. Batch Import - Batch import point data from files