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.

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.tsimport { 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));
} The React version uses the library's React adapter, cc-openlayers/react: each component creates the OpenLayers object it names when it mounts, updates it from its props and disposes of it when it unmounts.
map-position.react.tsximport { useRef, useState, type ChangeEvent } from 'react';
import type OLView from 'ol/View.js';
import type { Coordinate as OLCoordinate } from 'ol/coordinate.js';
import { toLonLat } from 'ol/proj.js';
import { Coordinate, DefaultControls, DefaultInteractions, Map, OSMSource, TileLayer, View } from 'cc-openlayers/react';
function number(event: ChangeEvent<HTMLInputElement>): number {
return event.target.valueAsNumber;
}
function round(value: number, decimals: number): number {
return Number(value.toFixed(decimals));
}
/** The view's centre and zoom as number inputs: typing moves the map, and the map's movements update them. */
export function MapPosition() {
const [lon, setLon] = useState(19.9368);
const [lat, setLat] = useState(50.0614);
const [zoom, setZoom] = useState(13);
const [moving, setMoving] = useState(false);
const view = useRef<OLView>(null);
const readView = () => {
// The view's inputs give it a centre and a zoom.
const current = view.current as OLView;
const [longitude, latitude] = toLonLat(current.getCenter() as OLCoordinate);
setLon(round(longitude, 4));
setLat(round(latitude, 4));
setZoom(round(current.getZoom() as number, 2));
setMoving(false);
};
return (
<>
<Map height="480px" onMoveStart={() => setMoving(true)} onMoveEnd={readView}>
<DefaultInteractions />
<DefaultControls />
<View ref={view} zoom={zoom}>
<Coordinate x={lon} y={lat} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
</Map>
<div className="example-controls">
<label>
Longitude
<input type="number" step="0.0001" value={lon} onChange={(event) => setLon(number(event))} />
</label>
<label>
Latitude
<input type="number" step="0.0001" value={lat} onChange={(event) => setLat(number(event))} />
</label>
<label>
Zoom
<input type="number" step="1" value={zoom} onChange={(event) => setZoom(number(event))} />
</label>
<output>{moving ? 'Moving' : 'Idle'}</output>
</div>
</>
);
}How the React version works
Number fields show the view's longitude, latitude and zoom. Typing a value moves the map, and the end of a map move writes its position back into the fields; an <output> shows “Moving” while the map moves. It is a two-way binding between React state and an OpenLayers object.
What both versions share
OpenLayers renders the map and handles the interactions; the cc-openlayers/core layer gives both adapters the same view binding (MapViewBinding) and the same conversion of coordinates from EPSG:4326 to the map's projection. The position is read with OpenLayers' toLonLat.
From Angular to React
| Angular | React |
|---|---|
signal for lon, lat, zoom and moving | four useState calls |
(moveStart), (moveEnd) on aol-map | onMoveStart, onMoveEnd on Map |
viewChild.required(ViewComponent).instance | useRef<OLView>(null) passed to View as ref |
[x]="lon()", [zoom]="zoom()" | x={lon}, zoom={zoom} |
Who creates, updates and disposes
Map creates the ol/Map in useLayoutEffect and disposes of it on unmount; each layer, source and interaction component adds its object to the parent it gets from context and removes it in its effect's cleanup, as ngOnDestroy does in Angular. After moveend the component stores the rounded centre (4 decimals) and zoom (2 decimals) in state; the next render passes them to View and Coordinate, which set the view only when a value changes.
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,latandzoomstate signals bound toaol-viewand itsaol-coordinatechild. (moveStart)and(moveEnd)onaol-maptrack 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 viainstance.- 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;
moveendfires 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.
Related examples
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.