Essentials · Angular, React and OpenLayers

Map position in signals

Synchronize OpenLayers map center and zoom two-way with Angular signals or React state using number inputs, toLonLat conversion and moveend events.

A street map of Nowa Huta in Kraków with the inputs longitude 20.0373, latitude 50.0718 and zoom 14 below it.
A screenshot of the example. Map data © OpenStreetMap contributors.

What this example shows

Opening at zoom 13 over central Kraków, the map sits above three editable number fields for longitude, latitude and zoom level. Editing any number jumps the camera to the new coordinate or scale immediately, while dragging, wheel-zooming or pinching the map writes the settled camera position back into all three inputs.

Active camera gestures are tracked cleanly between start and finish so the form updates once with readable, rounded values as soon as movement stops.

When you need it

WebGIS applications routinely mirror camera state outside the canvas—persisting the user’s last viewport in local storage, syncing coordinates into shareable URL parameters or providing a coordinate jump form for surveyors.

Two-way binding a 60 fps interactive canvas requires care: pushing camera coordinates into state on every animation frame thrashes change detection, and feeding unrounded floating-point coordinates back into view inputs can trigger unwanted micro-movements.

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.

map-position.ts
import { ChangeDetectionStrategy, Component, signal, viewChild } from '@angular/core';
import type { Coordinate } from 'ol/coordinate';
import { toLonLat } from 'ol/proj';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerTileComponent,
  MapComponent,
  SourceOsmComponent,
  ViewComponent,
} from 'cc-openlayers';

/** The view's centre and zoom as number inputs: typing moves the map, and the map's movements update them. */
@Component({
  selector: 'app-map-position',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerTileComponent,
    MapComponent,
    SourceOsmComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px" (moveStart)="moving.set(true)" (moveEnd)="readView()">
      <aol-interaction-default />
      <aol-control-defaults />
      <aol-view [zoom]="zoom()">
        <aol-coordinate [x]="lon()" [y]="lat()" srid="EPSG:4326" />
      </aol-view>
      <aol-layer-tile><aol-source-osm /></aol-layer-tile>
    </aol-map>
    <div class="example-controls">
      <label>
        Longitude
        <input type="number" step="0.0001" [value]="lon()" (input)="lon.set(number($event))" />
      </label>
      <label>
        Latitude
        <input type="number" step="0.0001" [value]="lat()" (input)="lat.set(number($event))" />
      </label>
      <label>
        Zoom
        <input type="number" step="1" [value]="zoom()" (input)="zoom.set(number($event))" />
      </label>
      <output>{{ moving() ? 'Moving' : 'Idle' }}</output>
    </div>
  `,
})
export class MapPosition {
  readonly lon = signal(19.9368);
  readonly lat = signal(50.0614);
  readonly zoom = signal(13);
  readonly moving = signal(false);
  private readonly view = viewChild.required(ViewComponent);

  protected readView(): void {
    // The view's inputs give it a centre and a zoom.
    const view = this.view().instance;
    const [lon, lat] = toLonLat(view.getCenter() as Coordinate);
    this.lon.set(round(lon, 4));
    this.lat.set(round(lat, 4));
    this.zoom.set(round(view.getZoom() as number, 2));
    this.moving.set(false);
  }

  protected number(event: Event): number {
    return (event.target as HTMLInputElement).valueAsNumber;
  }
}

function round(value: number, decimals: number): number {
  return Number(value.toFixed(decimals));
}

Architecture and OpenLayers mechanics

The component separates incoming form edits from outgoing camera notifications. Forward bindings connect the lon, lat and zoom signals to aol-view and aol-coordinate. In the reverse direction, (moveStart) and (moveEnd) on aol-map bracket user gestures and programmatic animations alike.

Once (moveEnd) fires, the handler accesses the live OpenLayers view via viewChild.required(ViewComponent) and its instance, converts projected Web Mercator metres to geographic degrees with toLonLat, and rounds coordinates to four decimal places (~11 m) and zoom to two decimals before writing back to state.

How it works

  • Editable number inputs update the lon, lat and zoom state signals bound to aol-view and its aol-coordinate child.
  • (moveStart) and (moveEnd) on aol-map track camera motion and read back the authoritative centre and zoom when the gesture settles.
  • viewChild.required(ViewComponent) provides typed access to the underlying OpenLayers view via instance.
  • Rounding longitude and latitude to four decimals and zoom to two decimals keeps form values clean and prevents feedback loops.

Pitfalls

  • Updating application state on every render frame floods change detection and causes input jitter; moveend fires once per completed gesture or animation.
  • Without toLonLat, reading the view centre directly returns Web Mercator metres in the millions rather than WGS84 degrees expected by geographic forms.
  • Ensure programmatic jumps and user drags share the same rounding precision so round-tripping a coordinate does not shift the view slightly.

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.