Interactions · Angular, React and OpenLayers
Draw a rectangle and read its GeoJSON
Draw a two-click bounding rectangle on an OpenLayers map using createBox() and export a cloned geometry as WGS84 GeoJSON in Angular and React.

What this example shows
Toggling the Draw button activates rectangle sketching over Kraków: clicking once sets the first corner, moving the pointer previews the expanding box, and clicking a second time completes the rectangle, renders it on a dedicated vector layer, and prints its geographic EPSG:4326 GeoJSON document below the map.
When you need it
Area-of-interest selectors, data-export clippers, report order forms and spatial search filters start by letting the user drag or click a clean rectangular box on the map and serialising that box as standard WGS84 GeoJSON for backend APIs.
Cloning the drawn Web Mercator geometry before transforming to EPSG:4326 preserves the on-map shape while giving forms clean degree coordinates.
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-polygon.tsimport { JsonPipe } from '@angular/common';
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import type { Feature as GeoJsonFeature, Polygon as GeoJsonPolygon } from 'geojson';
import Polygon from 'ol/geom/Polygon';
import { createBox, type DrawEvent } from 'ol/interaction/Draw';
import {
CollectionCoordinatesComponent,
CoordinateComponent,
DefaultInteractionComponent,
FeatureComponent,
GeometryPolygonComponent,
LayerTileComponent,
LayerVectorComponent,
MapComponent,
SourceOsmComponent,
SourceVectorComponent,
ViewComponent,
} from 'cc-openlayers';
import { DrawInteractionComponent } from 'cc-openlayers/editing';
/** A rectangle drawn with the draw interaction and shown as geographic GeoJSON. */
@Component({
selector: 'app-draw-polygon',
imports: [
CollectionCoordinatesComponent,
CoordinateComponent,
DefaultInteractionComponent,
DrawInteractionComponent,
FeatureComponent,
GeometryPolygonComponent,
JsonPipe,
LayerTileComponent,
LayerVectorComponent,
MapComponent,
SourceOsmComponent,
SourceVectorComponent,
ViewComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<aol-map height="480px">
<aol-interaction-default />
@if (drawing()) {
<aol-interaction-draw type="Circle" [geometryFunction]="box" (drawEnd)="drawEnd($event)" />
}
<aol-view [zoom]="13">
<aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" />
</aol-view>
<aol-layer-tile><aol-source-osm /></aol-layer-tile>
@if (drawn(); as drawn) {
<aol-layer-vector>
<aol-source-vector>
<aol-feature>
<aol-geometry-polygon>
<aol-collection-coordinates [coordinates]="drawn.geometry.coordinates" srid="EPSG:4326" />
</aol-geometry-polygon>
</aol-feature>
</aol-source-vector>
</aol-layer-vector>
}
</aol-map>
<div class="example-controls">
<button type="button" [attr.aria-pressed]="drawing()" (click)="drawing.set(!drawing())">Draw</button>
</div>
@if (drawn(); as drawn) {
<pre>{{ drawn | json }}</pre>
}
`,
})
export class DrawPolygon {
readonly drawing = signal(false);
readonly drawn = signal<GeoJsonFeature<GeoJsonPolygon> | undefined>(undefined);
protected readonly box = createBox();
protected drawEnd(event: DrawEvent): void {
// A box: the draw interaction's geometry function makes a polygon. A clone keeps it in map coordinates.
const geometry = event.feature.getGeometry() as Polygon;
const geographic = geometry.clone().transform('EPSG:3857', 'EPSG:4326');
this.drawn.set({
type: 'Feature',
properties: {},
geometry: { type: 'Polygon', coordinates: geographic.getCoordinates() },
});
}
} 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-polygon.react.tsximport { useState } from 'react';
import type { Feature as GeoJsonFeature, Polygon as GeoJsonPolygon } from 'geojson';
import type Polygon from 'ol/geom/Polygon.js';
import { createBox, type DrawEvent } from 'ol/interaction/Draw.js';
import {
CollectionCoordinates,
Coordinate,
DefaultInteractions,
DrawInteraction,
Feature,
Map,
OSMSource,
PolygonGeometry,
TileLayer,
VectorLayer,
VectorSource,
View,
} from 'cc-openlayers/react';
const box = createBox();
/** A rectangle drawn with the draw interaction and shown as geographic GeoJSON. */
export function DrawPolygon() {
const [drawing, setDrawing] = useState(false);
const [drawn, setDrawn] = useState<GeoJsonFeature<GeoJsonPolygon> | undefined>(undefined);
const drawEnd = (event: DrawEvent) => {
// A box: the draw interaction's geometry function makes a polygon. A clone keeps it in map coordinates.
const geometry = event.feature.getGeometry() as Polygon;
const geographic = geometry.clone().transform('EPSG:3857', 'EPSG:4326');
setDrawn({
type: 'Feature',
properties: {},
geometry: { type: 'Polygon', coordinates: geographic.getCoordinates() },
});
};
return (
<>
<Map height="480px">
<DefaultInteractions />
{drawing ? <DrawInteraction type="Circle" geometryFunction={box} onDrawEnd={drawEnd} /> : null}
<View zoom={13}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
{drawn ? (
<VectorLayer>
<VectorSource>
<Feature>
<PolygonGeometry>
<CollectionCoordinates coordinates={drawn.geometry.coordinates} srid="EPSG:4326" />
</PolygonGeometry>
</Feature>
</VectorSource>
</VectorLayer>
) : null}
</Map>
<div className="example-controls">
<button type="button" aria-pressed={drawing} onClick={() => setDrawing(!drawing)}>
Draw
</button>
</div>
{drawn ? <pre>{JSON.stringify(drawn, null, 2)}</pre> : null}
</>
);
}How the React version works
A Draw button switches on a draw interaction that sketches a rectangle from two corners; the drawn polygon is shown as geographic GeoJSON below the map while the feature stays in map coordinates.
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 button's (click) and @if around <aol-interaction-draw> | onClick toggling useState and a conditional <DrawInteraction> |
(drawEnd) writing the GeoJSON into a signal | onDrawEnd writing it into useState |
<aol-feature> with <aol-geometry-polygon> showing the result | <Feature> with <PolygonGeometry> and <CollectionCoordinates> |
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
A boolean signal controls @if around aol-interaction-draw, configured with type="Circle" and OpenLayers’ createBox() geometryFunction so two clicks produce an axis-aligned rectangular Polygon.
In the drawEnd handler, the component clones the sketch geometry, transforms the clone from EPSG:3857 to EPSG:4326 for GeoJSON serialisation (displayed via Angular’s json pipe), and stores those WGS84 coordinates in state, from which the vector layer reprojects and renders the feature.
How it works
- Toggling the Draw button mounts or removes
aol-interaction-drawcleanly via@if. - Pairing
type="Circle"withcreateBox()asgeometryFunctionturns a two-click centre/radius gesture into a corner-to-corner rectangle. - On
drawEnd, transforming aclone()of the geometry intoEPSG:4326leaves the on-map feature safely in Web Mercator. - Reactive state feeds both the persistent vector feature on the map and the formatted GeoJSON readout via the
jsonpipe.
Pitfalls
- Never call
.transform('EPSG:3857', 'EPSG:4326')directly on the live feature geometry attached to a Web Mercator layer—mutating it in place shrinks and teleports the drawn rectangle to 0°, 0°; always.clone()first. - OpenLayers’ Draw interaction discards its temporary sketch overlay as soon as
drawEndfinishes—bind a target vectorsourceor copy the finished feature into component state so the shape stays visible.
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.