Essentials · Angular, React and OpenLayers

The map extent after every move

Compute the visible geographic bounding box (west, east, south, north) of an OpenLayers map after every pan or zoom using moveend and calculateExtent.

A map of southern Poland and its neighbours at zoom 6, centred on Kraków, with its extent below it: left 9.41, right 30.46, bottom 46.56 and top 53.33 degrees.
A screenshot of the example. Map data © OpenStreetMap contributors.

What this example shows

Centred on Kraków at regional zoom 6 across southern Poland and neighbouring countries, the page displays four read-only coordinate fields (left, right, bottom and top) directly beneath the map.

Calculated once on initial load and refreshed whenever a pan, wheel zoom, pinch or animation settles, the four numbers report the exact WGS84 degree bounds of the currently visible map window.

When you need it

Spatial bounding-box queries power almost every scalable WebGIS screen: fetching features inside the current viewport, filtering a side-panel table to visible markers, or showing regional summary counts.

Waiting until the camera comes to rest on moveend ensures expensive spatial database queries, WFS requests or list recomputations execute once per user gesture rather than dozens of times mid-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.

moveend.ts
import { ChangeDetectionStrategy, Component, signal, viewChild } from '@angular/core';
import { getBottomLeft, getTopRight } from 'ol/extent';
import { toLonLat } from 'ol/proj';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerTileComponent,
  MapComponent,
  SourceOsmComponent,
  ViewComponent,
} from 'cc-openlayers';

/** The map's extent in decimal degrees, read-only fields updated whenever a move of the map ends. */
@Component({
  selector: 'app-moveend',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerTileComponent,
    MapComponent,
    SourceOsmComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px" (moveEnd)="showExtent()">
      <aol-interaction-default />
      <aol-control-defaults />
      <aol-view [zoom]="6">
        <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">
      <label>left: <input name="left" readonly [value]="left()" /></label>
      <label>right: <input name="right" readonly [value]="right()" /></label>
      <label>bottom: <input name="bottom" readonly [value]="bottom()" /></label>
      <label>top: <input name="top" readonly [value]="top()" /></label>
    </div>
  `,
})
export class Moveend {
  readonly left = signal('');
  readonly right = signal('');
  readonly bottom = signal('');
  readonly top = signal('');
  private readonly map = viewChild.required(MapComponent);

  protected showExtent(): void {
    const map = this.map().instance;
    const extent = map.getView().calculateExtent(map.getSize());
    const bottomLeft = toLonLat(getBottomLeft(extent));
    const topRight = toLonLat(getTopRight(extent));
    this.left.set(bottomLeft[0].toFixed(2));
    this.bottom.set(bottomLeft[1].toFixed(2));
    this.right.set(topRight[0].toFixed(2));
    this.top.set(topRight[1].toFixed(2));
  }
}

Architecture and OpenLayers mechanics

When (moveEnd) fires on aol-map, showExtent() calls calculateExtent(map.getSize()) on the view to obtain [minX, minY, maxX, maxY] in projected Web Mercator metres for the live viewport pixel size. getBottomLeft and getTopRight extract the southwest and northeast corners, and toLonLat converts both corners into decimal degrees.

Formatting the four bounds with toFixed(2) rounds them to hundredths of a degree (~1 km), keeping UI indicators concise and HTTP query URLs cache-friendly.

How it works

  • (moveEnd) on aol-map runs showExtent() immediately after initial render and after every finished pan, zoom or camera animation.
  • calculateExtent(map.getSize()) computes the projected Web Mercator bounding box matching the map container’s current pixel dimensions.
  • getBottomLeft and getTopRight read the opposite extent corners before toLonLat transforms them into geographic degrees.
  • Signals hold the four degree boundaries and display them rounded via toFixed(2) in read-only inputs.

Pitfalls

  • Rotating the map (for instance with Alt+Shift + drag) makes the axis-aligned box returned by calculateExtent(map.getSize()) larger than the tilted screen rectangle.
  • When a zoomed-out view crosses the 180° antimeridian, toLonLat wraps the eastern edge to a negative longitude so left exceeds right numerically; split antimeridian boxes into two queries before sending them to spatial APIs.
  • Because programmatic camera moves also emit moveend, never move the view unconditionally inside its own moveend listener or you will trigger an infinite loop.

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.