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.

A map of Kraków with points on the Barbican and Wawel, a line from Wawel into Kazimierz and a polygon over the Old Town, all in yellow, and Polygon below the map.
A screenshot of the example. Map data © OpenStreetMap contributors.

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.ts
import { 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;
  }
}

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-modify edits existing source features while aol-interaction-draw appends newly sketched geometries to the same source.
  • Recreating Draw and Snap together via @for and @if ensures 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.

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.