Interactions · Angular, React and OpenLayers
Measure distances and areas
Measure spherical route distances and land-parcel areas in metric or imperial units using cc-openlayers aol-interaction-measure in Angular.

What this example shows
Over a street map of Kraków, toggle buttons switch cleanly between distance (LineString) and area (Polygon) modes as well as metric and imperial units (aria-pressed), displaying a live measurement badge beside the cursor while sketching, locking the final formatted measurement below the map on completion, and clearing all sketches with one click.
When you need it
Every geoportal, parcel explorer, engineering viewer and hiking planner needs a dependable ruler and area planimeter right on the map toolbar.
Using the dedicated aol-interaction-measure component from cc-openlayers/measure provides spherical measurement math, live tooltips and unit formatting out of the box without boilerplate overlay management.
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.
measure.tsimport { ChangeDetectionStrategy, Component, signal, viewChild } from '@angular/core';
import {
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerTileComponent,
MapComponent,
SourceOsmComponent,
ViewComponent,
} from 'cc-openlayers';
import { MeasureInteractionComponent, MeasureType, type MeasureUnit } from 'cc-openlayers/measure';
/** Lengths and areas measured on the sphere with the library's measure interaction, in metric or imperial units. */
@Component({
selector: 'app-measure',
imports: [
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerTileComponent,
MapComponent,
MeasureInteractionComponent,
SourceOsmComponent,
ViewComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<aol-map height="480px">
<aol-interaction-default />
<aol-control-defaults />
<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>
<aol-interaction-measure
[type]="type()"
[unit]="unit()"
[showHelpTooltip]="true"
(measureComplete)="result.set($event.formattedMeasure)"
/>
</aol-map>
<div class="example-controls">
<span>
<button type="button" [attr.aria-pressed]="type() === 'LineString'" (click)="type.set('LineString')">
Distance
</button>
<button type="button" [attr.aria-pressed]="type() === 'Polygon'" (click)="type.set('Polygon')">Area</button>
</span>
<span>
<button type="button" [attr.aria-pressed]="unit() === 'metric'" (click)="unit.set('metric')">Metric</button>
<button type="button" [attr.aria-pressed]="unit() === 'imperial'" (click)="unit.set('imperial')">
Imperial
</button>
</span>
<button type="button" (click)="clear()">Clear</button>
<!-- The formatted area carries <sup>2</sup>. -->
<output [innerHTML]="result()"></output>
</div>
`,
})
export class Measure {
readonly type = signal<MeasureType>(MeasureType.LineString);
readonly unit = signal<MeasureUnit>('metric');
readonly result = signal('–');
private readonly measure = viewChild.required(MeasureInteractionComponent);
clear(): void {
this.measure().clearMeasurements();
this.result.set('–');
}
} 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.
measure.react.tsximport { useRef, useState } from 'react';
import DOMPurify from 'dompurify';
import {
Coordinate,
DefaultControls,
DefaultInteractions,
Map,
MeasureInteraction,
OSMSource,
TileLayer,
View,
type MeasureInteractionHandle,
} from 'cc-openlayers/react';
import { MeasureType, type MeasureUnit } from 'cc-openlayers/core';
/** Lengths and areas measured on the sphere with the library's measure interaction, in metric or imperial units. */
export function Measure() {
const [type, setType] = useState<MeasureType>(MeasureType.LineString);
const [unit, setUnit] = useState<MeasureUnit>('metric');
const [result, setResult] = useState('–');
const measure = useRef<MeasureInteractionHandle>(null);
const clear = () => {
measure.current?.clear();
setResult('–');
};
return (
<>
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
<View zoom={13}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
<MeasureInteraction
ref={measure}
type={type}
unit={unit}
showHelpTooltip={true}
onMeasureComplete={(measured) => setResult(measured.formattedMeasure)}
/>
</Map>
<div className="example-controls">
<span>
<button type="button" aria-pressed={type === 'LineString'} onClick={() => setType(MeasureType.LineString)}>
Distance
</button>
<button type="button" aria-pressed={type === 'Polygon'} onClick={() => setType(MeasureType.Polygon)}>
Area
</button>
</span>
<span>
<button type="button" aria-pressed={unit === 'metric'} onClick={() => setUnit('metric')}>
Metric
</button>
<button type="button" aria-pressed={unit === 'imperial'} onClick={() => setUnit('imperial')}>
Imperial
</button>
</span>
<button type="button" onClick={clear}>
Clear
</button>
{/* The formatted area carries <sup>2</sup>. */}
<output dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(result) }} />
</div>
</>
);
}How the React version works
Lengths and areas measured on the sphere with the library's measure interaction, in metric or imperial units; the finished measurement shows below the map and Clear removes it with the drawing.
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. The measure interaction and its types come from cc-openlayers/core.
From Angular to React
| Angular | React |
|---|---|
<aol-interaction-measure> with viewChild for Clear | <MeasureInteraction ref={measure}> with a MeasureInteractionHandle |
(measureComplete) and [innerHTML] for the result with its superscript | onMeasureComplete and dangerouslySetInnerHTML with DOMPurify.sanitize(…) |
the buttons' (click) | onClick with useState of the type and units |
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-measure encapsulates sketching, spherical distance/area calculation and live pointer overlays behind declarative type and unit inputs driven by accessible aria-pressed toggle buttons.
(measureComplete) emits the finished result into a reactive signal rendered below the map, while the Clear button invokes clearMeasurements() via viewChild.required.
How it works
aol-interaction-measurefromcc-openlayers/measurehandles line and polygon sketching with live spherical length and area readouts.- Accessible
aria-pressedtoggle pairs drive the interaction’stypeandunitbindings reactively. (measureComplete)delivers the formatted final measurement string into component state.- Querying the component with
viewChild.requiredlets Clear reset all drawn measurements viaclearMeasurements().
Pitfalls
- Never report raw planar .getLength() / .getArea() numbers from a Web Mercator geometry—at Polish latitudes planar Mercator distances are inflated by ~56% and areas by ~140%; always measure on the sphere or via
geodesicellipsoidal math. - Because formatted area strings include superscript HTML (
<sup>2</sup>for m² / km²), bind them through sanitizedinnerHTMLrather than plain text interpolation so literal tags are not shown.
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.