Layers · Angular, React and OpenLayers

Two maps with one view

Synchronize two side-by-side OpenLayers maps over Kraków—OpenStreetMap and satellite imagery—by sharing a single View instance via setView.

Two maps of Kraków next to each other: OpenStreetMap on the left and satellite imagery on the right.
A screenshot of the example. Map data © OpenStreetMap contributors. Imagery: Esri World Imagery (Esri, Maxar, Earthstar Geographics and the GIS User Community).

What this example shows

Two separate map panels sit side by side—OpenStreetMap street cartography on the left and Esri World Imagery on the right—framing the exact same streets of Kraków. Panning, wheel-zooming, pinching or rotating either panel moves its neighbour in instantaneous frame-for-frame lockstep without a trace of lag.

On narrow mobile viewports, CSS stacks the two panels vertically while preserving the shared camera link.

When you need it

Analysts comparing historical versus current orthophotos, street maps versus land-cover classifications, or alternative planning scenarios often want both full maps visible simultaneously rather than hidden behind a swipe curtain.

Sharing a single OpenLayers View instance across both maps eliminates error-prone event-copying code completely—every pan, zoom, rotation and animation is inherently identical on both canvases.

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.

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

/** Two maps side by side, OpenStreetMap and satellite imagery, that pan and zoom together through one shared view. */
@Component({
  selector: 'app-side-by-side',
  imports: [
    CoordinateComponent,
    DefaultInteractionComponent,
    LayerTileComponent,
    MapComponent,
    SourceOsmComponent,
    SourceXYZComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <div class="example-maps">
      <aol-map height="480px">
        <aol-interaction-default />
        <aol-view [zoom]="13">
          <aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" />
        </aol-view>
        <aol-layer-tile><aol-source-osm /></aol-layer-tile>
      </aol-map>
      <aol-map #imagery height="480px">
        <aol-interaction-default />
        <aol-layer-tile>
          <aol-source-xyz
            url="https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}"
          />
        </aol-layer-tile>
      </aol-map>
    </div>
  `,
})
export class SideBySide {
  private readonly view = viewChild.required(ViewComponent);
  private readonly imagery = viewChild.required('imagery', { read: MapComponent });

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

Architecture and OpenLayers mechanics

The left map declares aol-view normally in markup, while the right map omits aol-view altogether. Inside afterNextRender—once both map components have mounted in the browser—the component passes the first map’s underlying OpenLayers View reference straight into the second map’s setView method.

Responsive .example-maps layout rules place both viewports side by side on wide screens and stack them cleanly on phones.

How it works

  • Only the first map declares aol-view; the second map starts viewless and adopts the first map’s View instance.
  • afterNextRender waits until both map instances exist in the browser DOM before linking them with setView.
  • .example-maps arranges the two canvases side by side on desktop screens and stacks them vertically on narrow layouts.

Pitfalls

  • Never synchronise two maps by listening to move events and copying centre/zoom back and forth—event ping-pong introduces frame lag and feedback loops that a shared View object avoids completely.
  • Do not leave a declarative aol-view inside the second map when calling setView, or subsequent input bindings on that second child component will overwrite the shared View reference.

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.