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.

A street map of Kraków fitted to the extent of the zoom to extent button, labelled E below the zoom buttons, after a click on it.
A screenshot of the example. Map data © OpenStreetMap contributors.

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

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-zoomtoextent mounts OpenLayers’ E reset button directly beneath the zoom buttons of aol-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 extent option once at construction time; when the home bounds must change dynamically as user datasets load, wire a custom button calling view.fit instead.

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.