Interactions · Angular, React and OpenLayers

Move and resize a given box

Pre-populate a draggable, edge-resizable bounding box in OpenLayers while disabling new box creation via createCondition never and drag true.

A map of Kraków with a box over the Old Town and Wawel whose south-eastern corner has been dragged out to take in Kazimierz, and its handle at the new corner.
A screenshot of the example. Map data © OpenStreetMap contributors.

What this example shows

A crisp blue-outlined rectangular frame opens pre-positioned around Kraków’s Old Town and Wawel. Dragging anywhere inside the interior slides the entire frame across the city, dragging any edge or corner handle resizes it (for example expanding southeast over Kazimierz), and dragging outside the frame pans the map normally without ever accidentally replacing the box.

When you need it

Print-layout composers, fixed-quota imagery order forms and report-area selectors usually propose an initial bounding box centred on the user’s search address and ask the user only to nudge or resize that proposed frame.

Locking createCondition to never while enabling [drag]="true" turns ExtentInteraction into a dedicated frame-adjustment gizmo that never destroys the existing box on an accidental outside drag.

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.

extent-interaction-modify.ts
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { never } from 'ol/events/condition';
import { transformExtent } from 'ol/proj';
import Fill from 'ol/style/Fill';
import Stroke from 'ol/style/Stroke';
import Style from 'ol/style/Style';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  ExtentInteractionComponent,
  LayerTileComponent,
  MapComponent,
  SourceOsmComponent,
  ViewComponent,
} from 'cc-openlayers';

/** A box around the Old Town that the user moves and resizes with the extent interaction, but cannot draw anew. */
@Component({
  selector: 'app-extent-interaction-modify',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    ExtentInteractionComponent,
    LayerTileComponent,
    MapComponent,
    SourceOsmComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px">
      <aol-interaction-default />
      <aol-interaction-extent [extent]="extent" [createCondition]="never" [drag]="true" [boxStyle]="boxStyle" />
      <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-map>
    <div class="example-controls">
      <p class="example-hint">Drag the box to move it, or drag one of its edges or corners to resize it.</p>
    </div>
  `,
})
export class ExtentInteractionModify {
  /** The Old Town and Wawel, in the view's projection. */
  protected readonly extent = transformExtent([19.925, 50.048, 19.95, 50.068], 'EPSG:4326', 'EPSG:3857');
  /** A pointer down never starts a new box. */
  protected readonly never = never;
  protected readonly boxStyle = new Style({
    fill: new Fill({ color: 'rgba(255, 255, 255, 0.4)' }),
    stroke: new Stroke({ color: '#3399CC', width: 1.25 }),
  });
}

Architecture and OpenLayers mechanics

Initial WGS84 degree bounds convert to Web Mercator via transformExtent and bind to [extent], styled with a clear blue-rimmed boxStyle.

Setting [drag]="true" enables interior whole-box translation, while setting createCondition to never blocks outside drags from starting a replacement rectangle—letting outside drags fall straight through to normal map panning.

How it works

  • [extent] initialises the pre-populated bounding box projected from degrees via transformExtent.
  • [drag]="true" translates the entire rectangle whenever a drag begins inside its interior away from the edges.
  • Setting createCondition to never prevents drags outside the box from replacing it and preserves normal map panning.
  • boxStyle applies a clear blue outline and translucent white fill so edges and handles stand out over street maps.

Pitfalls

  • If you enable [drag]="true" without setting createCondition to never, dragging outside the box defaults to always—wiping out the user’s frame and drawing a new box instead of panning the map.
  • Because edge/corner snapping claims a 10-pixel band around the perimeter, boxes shrunk narrower than ~20 pixels across expose only resize handles until enlarged again.

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.