Controls · Angular, React and OpenLayers
Zoom to an extent with a button
Add an OpenLayers ZoomToExtent control button below the zoom bar so one click fits the camera back to Kraków’s Web Mercator bounds anytime.

What this example shows
Beneath the standard + and − zoom buttons on a street map of Kraków sits an additional E (Zoom to Extent) control button. No matter how far across Europe you pan or how deeply you zoom into a single street, clicking E immediately frames the greater Kraków metropolitan box cleanly inside the viewport.
When you need it
In municipal portals, campus maps and engineering site viewers, users frequently zoom in to inspect details or pan afield and want a single-click “Home / Fit project area” button right in the primary navigation cluster.
Placing ZoomToExtent directly beneath the zoom buttons uses familiar GIS conventions and saves users from hunting through menus or reloading the page to recover their starting context.
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.
navigation-controls.tsimport { ChangeDetectionStrategy, Component } from '@angular/core';
import {
ControlZoomToExtentComponent,
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerTileComponent,
MapComponent,
SourceOsmComponent,
ViewComponent,
} from 'cc-openlayers';
/** The default zoom control with a zoom to extent control that returns the view to the Kraków area. */
@Component({
selector: 'app-navigation-controls',
imports: [
ControlZoomToExtentComponent,
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerTileComponent,
MapComponent,
SourceOsmComponent,
ViewComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<aol-map height="480px">
<aol-interaction-default />
<aol-control-defaults />
<aol-control-zoomtoextent [extent]="[2204126, 6444544, 2243088, 6468820]" />
<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>
`,
})
export class NavigationControls {} 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.
navigation-controls.react.tsximport {
Coordinate,
DefaultControls,
DefaultInteractions,
Map,
OSMSource,
TileLayer,
View,
ZoomToExtentControl,
} from 'cc-openlayers/react';
const extent = [2204126, 6444544, 2243088, 6468820];
/** The default zoom control with a zoom to extent control that returns the view to the Kraków area. */
export function NavigationControls() {
return (
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
<ZoomToExtentControl extent={extent} />
<View zoom={6}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
</Map>
);
}How the React version works
Beside the default zoom there is a button that fits the view to the extent of Kraków.
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.
From Angular to React
| Angular | React |
|---|---|
<aol-control-zoomtoextent [extent]> | <ZoomToExtentControl extent={extent} /> with a module constant |
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 control is a child of Map, as in the Angular template; the adapter adds it to the map and removes it on unmount.
Architecture and OpenLayers mechanics
aol-control-zoomtoextent instantiates OpenLayers’ ZoomToExtent widget alongside aol-control-defaults. Its [extent] input defines the target bounding box in the view’s native Web Mercator metres (converted from 19.80°–20.15° E and 49.99°–50.13° N around Kraków).
Clicking the button computes the coarsest resolution at which the entire extent fits inside the current viewport dimensions and jumps straight to that centred view; omitting [extent] falls back to fitting the entire projection world.
How it works
aol-control-zoomtoextentmounts OpenLayers’Ereset button directly beneath the zoom buttons ofaol-control-defaults.[extent]supplies the target Kraków bounding box in projected Web Mercator metres (covering 19.80°–20.15° E, 49.99°–50.13° N).- Clicking the control fits the full target box instantaneously into the current map size without intermediate animation.
- If
[extent]is omitted, the control defaults to fitting the full Web Mercator world projection extent.
Pitfalls
- Passing raw WGS84 degree numbers ([19.80, 49.99, 20.15, 50.13]) on a Web Mercator view defines a sub-metre rectangle near 0°, 0° in the Atlantic Ocean, causing clicks to zoom to maximum scale over empty water.
- ZoomToExtent captures its
extentoption once at construction time; when the home bounds must change dynamically as user datasets load, wire a custom button calling view.fit instead.
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.