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.

A street map of central Kraków with the longitude and latitude of the pointer below it.
A screenshot of the example. Map data © OpenStreetMap contributors.

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.ts
import { 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]) : '–';
  }
}

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) on aol-map supplies the live map browser event; toLonLat converts its projected coordinate into wrapped geographic degrees.
  • A nullable coordinate signal feeds two computed values rendered inside accessible <output> elements below the map.
  • formatDegrees prints 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 toLonLat reports 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.

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.