Essentials · Angular, React and OpenLayers
Cursor coordinates outside the map
Stream live WGS84 longitude and latitude under the pointer into UI elements outside an OpenLayers map using pointermove, toLonLat and signals.

What this example shows
Moving the mouse or touch pointer across a street map of central Kraków updates semantic <output> read-outs below the canvas with the exact WGS84 longitude and latitude under the cursor. Until the pointer enters the map for the first time, clean dashes are shown instead of a misleading zero coordinate.
Coordinates are formatted to six decimal places—roughly 11 centimetres on the ground—matching the typography and layout of the surrounding application.
When you need it
Planners, surveyors, emergency dispatchers and support engineers constantly inspect cursor coordinates to verify parcel corners, log field assets or cross-check external GPS records.
Rendering coordinates in your own Angular or React template rather than OpenLayers’ default on-canvas widget lets you integrate the read-out into status bars, inspection drawers or copy-to-clipboard toolbars with full design-system styling and screen-reader semantics.
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.
cursor-position.tsimport { ChangeDetectionStrategy, Component, computed, signal } from '@angular/core';
import type MapBrowserEvent from 'ol/MapBrowserEvent';
import { toLonLat } from 'ol/proj';
import {
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerTileComponent,
MapComponent,
SourceOsmComponent,
ViewComponent,
} from 'cc-openlayers';
/** Degrees with six decimals, about a tenth of a metre; a value that rounds to zero loses its minus sign. */
export function formatDegrees(value: number): string {
const text = value.toFixed(6);
return text === '-0.000000' ? '0.000000' : text;
}
/** The geographic position under the pointer, read from the map's pointer-move events. */
@Component({
selector: 'app-cursor-position',
imports: [
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerTileComponent,
MapComponent,
SourceOsmComponent,
ViewComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<aol-map height="480px" (pointerMove)="track($event)">
<aol-interaction-default />
<aol-control-defaults />
<aol-view [zoom]="14">
<aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" />
</aol-view>
<aol-layer-tile><aol-source-osm /></aol-layer-tile>
</aol-map>
<div class="example-controls">
<label>
Longitude
<output>{{ lon() }}</output>
</label>
<label>
Latitude
<output>{{ lat() }}</output>
</label>
</div>
`,
})
export class CursorPosition {
private readonly position = signal<number[] | undefined>(undefined);
readonly lon = computed(() => this.format(0));
readonly lat = computed(() => this.format(1));
protected track(event: MapBrowserEvent): void {
this.position.set(toLonLat(event.coordinate));
}
private format(axis: number): string {
const position = this.position();
return position ? formatDegrees(position[axis]) : '–';
}
} 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.
cursor-position.react.tsximport { useState } from 'react';
import type MapBrowserEvent from 'ol/MapBrowserEvent.js';
import { toLonLat } from 'ol/proj.js';
import { Coordinate, DefaultControls, DefaultInteractions, Map, OSMSource, TileLayer, View } from 'cc-openlayers/react';
/** Degrees with six decimals, about a tenth of a metre; a value that rounds to zero loses its minus sign. */
export function formatDegrees(value: number): string {
const text = value.toFixed(6);
return text === '-0.000000' ? '0.000000' : text;
}
/** The geographic position under the pointer, read from the map's pointer-move events. */
export function CursorPosition() {
const [position, setPosition] = useState<number[] | undefined>(undefined);
const format = (axis: number) => (position ? formatDegrees(position[axis]) : '–');
return (
<>
<Map height="480px" onPointerMove={(event: MapBrowserEvent) => setPosition(toLonLat(event.coordinate))}>
<DefaultInteractions />
<DefaultControls />
<View zoom={14}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
</Map>
<div className="example-controls">
<label>
Longitude
<output>{format(0)}</output>
</label>
<label>
Latitude
<output>{format(1)}</output>
</label>
</div>
</>
);
}How the React version works
Below the map, the longitude and latitude of the point under the cursor, to six decimals. It shows how a map event (pointermove) becomes component state.
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 formatDegrees function is the same in both versions (a value that rounds to zero loses its minus sign).
From Angular to React
| Angular | React |
|---|---|
(pointerMove) on aol-map | onPointerMove on Map |
signal<number[] | undefined> | useState<number[] | undefined> |
the template call format(0) | a format function in the component body |
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. The adapter subscribes onPointerMove once and always calls its latest version, so a new function on every render does not resubscribe.
Architecture and OpenLayers mechanics
(pointerMove) on aol-map emits OpenLayers’ map browser event containing the projected map coordinate under the pointer pixel. Passing that coordinate through toLonLat converts Web Mercator metres into WGS84 decimal degrees while automatically wrapping longitudes into the canonical −180° to +180° range across repeated world copies.
Storing the raw coordinate in a signal and deriving formatted strings via computed signals keeps high-frequency pointer updates fast and restricts DOM writes to the two <output> nodes.
How it works
(pointerMove)onaol-mapsupplies the live map browser event;toLonLatconverts its projected coordinate into wrapped geographic degrees.- A nullable coordinate signal feeds two
computedvalues rendered inside accessible<output>elements below the map. formatDegreesprints six decimal places (~0.1 m ground precision) and strips the minus sign from values that round to-0.000000.- Initialising state to empty displays an en dash before the first pointer movement rather than 0°, 0° in the Gulf of Guinea.
Pitfalls
- Transforming coordinates with a generic EPSG:4326 projection call without
toLonLatreports unwrapped longitudes beyond ±180° when the map pans across repeated worlds. - Pointer move events fire dozens of times per second during cursor travel: keep the handler allocation-free and never trigger network requests directly on pointer motion.
- On touch-only screens without a hovering cursor, pointer coordinates update during drag gestures; pair with a tap handler if mobile users need single-tap coordinate inspection.
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.