Interactions · Angular, React and OpenLayers
Draw and edit points, lines, polygons and circles
Combine OpenLayers Draw, Modify and Snap interactions on one shared vector source to sketch, connect and edit points, lines, areas and circles.

What this example shows
Over a street map of Kraków, a geometry-type selector lets you sketch yellow points, lines, polygons and circles that snap cleanly onto previously drawn shapes—while allowing you to grab and adjust any existing vertex or edge at any moment without switching tools.
When you need it
Complete digitising toolbars in GIS, CAD and field-inspection apps must let users sketch new features, snap boundaries cleanly to existing neighbours, and refine misplaced vertices on a single shared vector layer.
Wiring Modify, Draw and Snap together in the right registration order avoids gaps between adjacent zones and eliminates awkward mode toggles.
The code
The example uses cc-openlayers, my Angular component library for OpenLayers: each aol-… element creates the OpenLayers object it names, updates it from its inputs and disposes of it with the component.
draw-and-modify-features.tsimport { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import type { Extent } from 'ol/extent';
import type { FlatStyle } from 'ol/style/flat';
import {
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerTileComponent,
LayerVectorComponent,
MapComponent,
SourceOsmComponent,
SourceVectorComponent,
ViewComponent,
} from 'cc-openlayers';
import { DrawInteractionComponent, ModifyInteractionComponent, SnapInteractionComponent } from 'cc-openlayers/editing';
type DrawType = 'Point' | 'LineString' | 'Polygon' | 'Circle';
/** Features drawn of the chosen geometry type, snapped to the existing ones and edited with the modify interaction. */
@Component({
selector: 'app-draw-and-modify-features',
imports: [
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
DrawInteractionComponent,
LayerTileComponent,
LayerVectorComponent,
MapComponent,
ModifyInteractionComponent,
SnapInteractionComponent,
SourceOsmComponent,
SourceVectorComponent,
ViewComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<aol-map height="480px">
<aol-interaction-default />
<aol-control-defaults />
<aol-view [zoom]="13" [extent]="extent">
<aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" />
</aol-view>
<aol-layer-tile><aol-source-osm /></aol-layer-tile>
<aol-layer-vector [style]="style"><aol-source-vector #source /></aol-layer-vector>
<aol-interaction-modify [source]="source.instance" />
<!-- Draw and snap are made anew for each type; snap after draw, so it adjusts the pointer for it. -->
@for (current of types; track current) {
@if (current === type()) {
<aol-interaction-draw [type]="current" [source]="source.instance" />
<aol-interaction-snap [source]="source.instance" />
}
}
</aol-map>
<div class="example-controls">
<label>
Geometry type
<select (change)="type.set(value($event))">
@for (option of types; track option) {
<option [value]="option" [selected]="option === type()">{{ option }}</option>
}
</select>
</label>
</div>
`,
})
export class DrawAndModifyFeatures {
readonly type = signal<DrawType>('Point');
protected readonly types: DrawType[] = ['Point', 'LineString', 'Polygon', 'Circle'];
protected readonly style: FlatStyle = {
'fill-color': 'rgba(255, 255, 255, 0.2)',
'stroke-color': '#ffcc33',
'stroke-width': 2,
'circle-radius': 7,
'circle-fill-color': '#ffcc33',
};
/** One world to the east and to the west of the Web Mercator world; geometry coordinates stay within it. */
protected readonly extent: Extent = [-40075016.68, -20037508.34, 40075016.68, 20037508.34];
protected value(event: Event): DrawType {
return (event.target as HTMLSelectElement).value as DrawType;
}
} The React version uses the library's React adapter, cc-openlayers/react: each component creates the OpenLayers object it names when it mounts, updates it from its props and disposes of it when it unmounts.
draw-and-modify-features.react.tsximport { useState } from 'react';
import type { Extent } from 'ol/extent.js';
import type OLVectorSource from 'ol/source/Vector.js';
import type { FlatStyle } from 'ol/style/flat.js';
import {
Coordinate,
DefaultControls,
DefaultInteractions,
DrawInteraction,
Map,
ModifyInteraction,
OSMSource,
SnapInteraction,
TileLayer,
VectorLayer,
VectorSource,
View,
} from 'cc-openlayers/react';
type DrawType = 'Point' | 'LineString' | 'Polygon' | 'Circle';
const types: DrawType[] = ['Point', 'LineString', 'Polygon', 'Circle'];
const style: FlatStyle = {
'fill-color': 'rgba(255, 255, 255, 0.2)',
'stroke-color': '#ffcc33',
'stroke-width': 2,
'circle-radius': 7,
'circle-fill-color': '#ffcc33',
};
/** One world to the east and to the west of the Web Mercator world; geometry coordinates stay within it. */
const extent: Extent = [-40075016.68, -20037508.34, 40075016.68, 20037508.34];
/** Features drawn of the chosen geometry type, snapped to the existing ones and edited with the modify interaction. */
export function DrawAndModifyFeatures() {
const [type, setType] = useState<DrawType>('Point');
// The source is made after the first commit, so the state carries it to the interactions.
const [source, setSource] = useState<OLVectorSource | null>(null);
return (
<>
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
<View zoom={13} extent={extent}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
<VectorLayer style={style}>
<VectorSource ref={setSource} />
</VectorLayer>
{source ? <ModifyInteraction source={source} /> : null}
{/* Draw and snap are made anew for each type; snap after draw, so it adjusts the pointer for it. */}
{source ? (
<>
<DrawInteraction key={type} type={type} source={source} />
<SnapInteraction key={`snap-${type}`} source={source} />
</>
) : null}
</Map>
<div className="example-controls">
<label>
Geometry type
<select value={type} onChange={(event) => setType(event.target.value as DrawType)}>
{types.map((option) => (
<option key={option} value={option}>
{option}
</option>
))}
</select>
</label>
</div>
</>
);
}How the React version works
Features of the chosen geometry type drawn, snapped to the existing ones and edited with a modify interaction on the same source; the view stays within one world east and west.
What both versions share
OpenLayers renders the map and handles the interactions; the cc-openlayers/core layer gives both adapters the same view binding (MapViewBinding) and the same conversion of coordinates from EPSG:4326 to the map's projection.
From Angular to React
| Angular | React |
|---|---|
the type <select>'s (change) and a signal | onChange and useState |
viewChild of the source handed to the interactions | ref={setSource} into useState; the interactions render once the adapter has made the source |
@if around the draw and snap interactions, one modify interaction | <DrawInteraction key={type}> and <SnapInteraction key={snap-${type}}> replaced with the type, one <ModifyInteraction> kept; the snap is rendered last, as OpenLayers needs |
Who creates, updates and disposes
Map creates the ol/Map in useLayoutEffect and disposes of it on unmount; each layer, source and interaction component adds its object to the parent it gets from context and removes it in its effect's cleanup, as ngOnDestroy does in Angular.
Architecture and OpenLayers mechanics
aol-interaction-modify stays mounted permanently against the shared vector source so existing shapes remain editable at all times. Whenever the selected geometry type changes, @for and @if recreate aol-interaction-draw followed immediately by the snap interaction—guaranteeing Snap always sits last in OpenLayers’ interaction list so it can adjust pointer coordinates before Draw and Modify read them.
A view extent two worlds wide (±360°) simply limits how far the camera can pan; it does not constrain drawing to a single 360° world.
How it works
aol-interaction-modifyedits existing source features whileaol-interaction-drawappends newly sketched geometries to the same source.- Recreating Draw and Snap together via
@forand@ifensures Snap is always registered after the active Draw interaction. - A unified flat style renders yellow strokes, yellow point markers and translucent white polygon fills across all geometry types.
- A view extent two worlds wide (±360°) limits how far the camera can pan; it does not constrain the coordinates you draw.
Pitfalls
- Snap only adjusts pointer events for interactions registered before it: if Draw is recreated dynamically while Snap stays old, Snap sits ahead of Draw and stops snapping new sketches—recreate Snap right after Draw.
- OpenLayers’ Draw interaction reads its geometry type once at construction time, so switching between Point, LineString, Polygon and Circle requires recreating
aol-interaction-draw.
Related examples
The same feature in the OpenLayers examples .
Background: state and lifecycle of maps in Angular with OpenLayers, which the components of cc-openlayers take care of.