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.

A map of Kraków with a line measured from Bronowice past the Main Square and Kazimierz to Podgórze, its length, and the unit buttons below it.
A screenshot of the example. Map data © OpenStreetMap contributors.

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.ts
import { 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('–');
  }
}

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-measure from cc-openlayers/measure handles line and polygon sketching with live spherical length and area readouts.
  • Accessible aria-pressed toggle pairs drive the interaction’s type and unit bindings reactively.
  • (measureComplete) delivers the formatted final measurement string into component state.
  • Querying the component with viewChild.required lets Clear reset all drawn measurements via clearMeasurements().

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 geodesic ellipsoidal math.
  • Because formatted area strings include superscript HTML (<sup>2</sup> for m² / km²), bind them through sanitized innerHTML rather than plain text interpolation so literal tags are not shown.

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.