Essentials · Angular, React and OpenLayers

A minimum zoom that follows the width of the map

Compute OpenLayers minZoom dynamically from viewport width on initial render and resize so a world map fills every screen on crisp integer zoom levels.

A world map at zoom 2 centred on Kraków, with Poland filled red in the middle.
A screenshot of the example. Administrative boundaries © GUGiK. Map data © OpenStreetMap contributors.

What this example shows

A world overview centred on Kraków highlights Poland in red and locks its furthest zoom-out floor to the smallest integer zoom level at which a single 256-pixel Web Mercator world fills the container width. Resizing the browser window narrower or wider recomputes that floor dynamically so the user can never zoom out into empty grey margins or repeated side-by-side continents.

When you need it

Global choropleths, aviation trackers, climate dashboards and worldwide store locators need a responsive “whole world” zoom floor that adapts cleanly from mobile phones to ultrawide desktop monitors.

A hardcoded minimum zoom of 1 or 2 breaks across responsive breakpoints—either clipping continents on narrow screens or shrinking Earth into a tiny strip surrounded by repeated worlds on wide displays. Deriving the limit from actual pixel width guarantees edge-to-edge coverage on sharp whole tile levels.

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.

min-zoom.ts
import { ChangeDetectionStrategy, Component, ElementRef, afterNextRender, signal, viewChild } from '@angular/core';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerTileComponent,
  LayerVectorComponent,
  MapComponent,
  SourceGeoJSONComponent,
  SourceOsmComponent,
  ViewComponent,
} from 'cc-openlayers';

/** A view whose minimum zoom keeps one world in sight, recomputed when the window is resized; Poland is marked on it. */
@Component({
  selector: 'app-min-zoom',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerTileComponent,
    LayerVectorComponent,
    MapComponent,
    SourceGeoJSONComponent,
    SourceOsmComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: { '(window:resize)': 'measure()' },
  template: `
    <div #viewport>
      @if (minZoom() !== undefined) {
        <aol-map height="480px">
          <aol-interaction-default />
          <aol-control-defaults />
          <aol-view [zoom]="initialZoom()" [minZoom]="minZoom()">
            <aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" />
          </aol-view>
          <aol-layer-tile><aol-source-osm /></aol-layer-tile>
          <aol-layer-vector [style]="poland">
            <aol-source-geojson
              url="assets/poland/poland.geojson"
              featureProjection="EPSG:3857"
              attributions="Boundaries: PRG, GUGiK"
            />
          </aol-layer-vector>
        </aol-map>
      }
    </div>
    <div class="example-controls">
      <p class="example-hint">Zoom out as far as you can, then resize the window: the limit follows its width.</p>
    </div>
  `,
})
export class MinZoom {
  /** Poland in the red of its flag, to stand out on the world map. */
  protected readonly poland = { 'fill-color': 'rgba(220, 20, 60, 0.5)', 'stroke-color': '#dc143c', 'stroke-width': 1 };
  protected readonly minZoom = signal<number | undefined>(undefined);
  protected readonly initialZoom = signal(0);
  private readonly viewport = viewChild.required<ElementRef<HTMLElement>>('viewport');

  constructor() {
    afterNextRender(() => {
      this.initialZoom.set(this.zoomForWidth());
      this.measure();
    });
  }

  protected measure(): void {
    this.minZoom.set(this.zoomForWidth());
  }

  /** The zoom at which the world, 256 pixels wide at zoom 0, fills the width of the page. */
  private zoomForWidth(): number {
    return Math.max(0, Math.ceil(Math.log2(this.viewport().nativeElement.clientWidth / 256)));
  }
}

Architecture and OpenLayers mechanics

In a standard Web Mercator tile pyramid, zoom level 0 is 256 CSS pixels wide and each level doubles that width. Dividing the measured container width by 256, taking Math.log2 and rounding up with Math.ceil yields the first whole zoom level where one world is at least as wide as the viewport.

Because layout width is unavailable before initial DOM paint, measurement runs inside afterNextRender on startup, writes into a signal bound to [minZoom]="minZoom()", and refreshes through a (window:resize) host listener.

How it works

  • Since Web Mercator is 256 pixels wide at zoom 0 and doubles per level, Math.ceil of Math.log2 of (width / 256) gives the lowest integer zoom that fills the map width.
  • [minZoom]="minZoom()" binds the dynamic limit to the view while (window:resize) updates it whenever the browser window changes size.
  • afterNextRender defers the initial DOM width measurement until layout is complete and starts the view at that computed floor.
  • Rounding up to an integer level keeps base-map tiles at native 1:1 resolution instead of stopping at a blurry fractional scale.

Pitfalls

  • Window resize listeners do not fire when a collapsible sidebar or split-pane divider resizes the map container without changing the browser window; attach a ResizeObserver to the host element in panel-based layouts.
  • When a view enables multi-world wrapping, OpenLayers disables its internal single-world stop, making this explicit width-based floor essential to prevent horizontal world repetition.
  • Guard against zero-width reads if the component mounts inside a hidden tab or collapsed accordion.

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.