Essentials · Angular, React and OpenLayers

Preloading low-resolution tiles

Compare two synchronized OpenLayers maps sharing a single view to see how preloading coarse parent tiles prevents blank squares on zoom.

Two street maps of Kraków and Małopolska at zoom 9 side by side, sharing one view.
A screenshot of the example. Map data © OpenStreetMap contributors.

What this example shows

Two street maps of Kraków and Małopolska sit side by side sharing a single synchronised camera view: panning or zooming either map moves both identically. One map preloads all coarser parent zoom levels ([preload]="infinity"), while the companion map loads only the current zoom level ([preload]="0").

Rapidly zooming out or flick-panning across the region makes the trade-off immediately visible: the preloaded map fills uncovered areas instantly with soft low-resolution fallback tiles while sharp tiles stream in, whereas the zero-preload map briefly exposes blank background squares.

When you need it

Fast-moving interactive maps, guided fly-to tours and kiosk displays feel far smoother when rapid camera motion never flashes raw grey canvas cells.

Understanding the visual benefit versus extra HTTP request overhead lets teams tune tile preloading sensibly for desktop broadband versus metered mobile connections.

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.

preload.ts
import { afterNextRender, ChangeDetectionStrategy, Component, viewChild } from '@angular/core';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerTileComponent,
  MapComponent,
  SourceOsmComponent,
  ViewComponent,
} from 'cc-openlayers';

/** Two maps on one view, the upper preloading low resolution tiles and the lower loading none in advance. */
@Component({
  selector: 'app-preload',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerTileComponent,
    MapComponent,
    SourceOsmComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <div class="example-maps">
      <aol-map height="480px">
        <aol-interaction-default />
        <aol-control-defaults />
        <aol-view [zoom]="9">
          <aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" />
        </aol-view>
        <aol-layer-tile [preload]="infinity"><aol-source-osm /></aol-layer-tile>
      </aol-map>
      <aol-map #plain height="480px">
        <aol-interaction-default />
        <aol-control-defaults />
        <aol-layer-tile [preload]="0"><aol-source-osm /></aol-layer-tile>
      </aol-map>
    </div>
  `,
})
export class Preload {
  protected readonly infinity = Infinity;
  private readonly view = viewChild.required(ViewComponent);
  private readonly plain = viewChild.required('plain', { read: MapComponent });

  constructor() {
    afterNextRender(() => this.plain().instance.setView(this.view().instance));
  }
}

Architecture and OpenLayers mechanics

On any OpenLayers tile layer, preload is a count of coarser pyramid levels to keep cached beneath the active view: [preload]="infinity" fetches every ancestor level up to zoom 0, whereas [preload]="0" disables ancestor prefetching completely. Because Angular template expressions do not expose global Infinity directly, the component exposes Infinity through a class property infinity.

To guarantee an exact apples-to-apples visual comparison, the second map omits its own declarative view and attaches the first map’s view instance via setView after initial render.

How it works

  • [preload]="infinity" prefetches every coarser ancestor tile level for the visible area, while [preload]="0" fetches only the active zoom level.
  • Calling setView binds both maps to the exact same OpenLayers view instance so pans, zooms and animations run in lockstep.
  • Passing Infinity through a component property infinity keeps template binding syntax clean and type-safe.

Pitfalls

  • Setting preload to Infinity fetches every ancestor pyramid tier up front, which can double initial tile request counts on small viewports; in bandwidth-sensitive mobile apps, preload 2 to 4 coarser levels rather than all the way to zoom 0.

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.