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.

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.tsimport { 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));
}
} 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.
moveend.react.tsximport { useRef, useState } from 'react';
import type OLMap from 'ol/Map.js';
import { getBottomLeft, getTopRight } from 'ol/extent.js';
import { toLonLat } from 'ol/proj.js';
import { Coordinate, DefaultControls, DefaultInteractions, Map, OSMSource, TileLayer, View } from 'cc-openlayers/react';
/** The map's extent in decimal degrees, read-only fields updated whenever a move of the map ends. */
export function Moveend() {
const [left, setLeft] = useState('');
const [right, setRight] = useState('');
const [bottom, setBottom] = useState('');
const [top, setTop] = useState('');
const mapRef = useRef<OLMap>(null);
const showExtent = () => {
const map = mapRef.current as OLMap;
const extent = map.getView().calculateExtent(map.getSize());
const bottomLeft = toLonLat(getBottomLeft(extent));
const topRight = toLonLat(getTopRight(extent));
setLeft(bottomLeft[0].toFixed(2));
setBottom(bottomLeft[1].toFixed(2));
setRight(topRight[0].toFixed(2));
setTop(topRight[1].toFixed(2));
};
return (
<>
<Map ref={mapRef} height="480px" onMoveEnd={showExtent}>
<DefaultInteractions />
<DefaultControls />
<View zoom={6}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
</Map>
<div className="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>
</>
);
}How the React version works
After each pan or zoom, four read-only fields show the map's extent in degrees (left, right, bottom and top edge, two decimals).
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 extent comes from View.calculateExtent, the corners from getBottomLeft/getTopRight and the degrees from toLonLat.
From Angular to React
| Angular | React |
|---|---|
viewChild(MapComponent).instance | useRef<OLMap>(null) passed to Map as ref |
(moveEnd)="showExtent()" | onMoveEnd={showExtent} |
four signal calls | four useState calls |
[value] + readonly | value + readOnly |
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. The map ref points to the ol/Map while the component is mounted; after unmount React sets it to null.
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)onaol-maprunsshowExtent()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.getBottomLeftandgetTopRightread the opposite extent corners beforetoLonLattransforms 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,
toLonLatwraps 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 ownmoveendlistener or you will trigger an infinite loop.
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.