Overlays and animation · Angular, React and OpenLayers

A popup at the clicked place

Open a dismissible coordinate popup at clicked map locations in OpenLayers with HDMS formatting and smooth 250 ms autoPan viewport margining.

A map of Kraków with a dark popup over the Main Square that reads You clicked here: 50° 03′ 42″ N 19° 56′ 14″ E, with a × button that closes it.
A screenshot of the example. Map data © OpenStreetMap contributors.

What this example shows

Clicking anywhere on the street map of Kraków opens a dark callout card anchored 8 pixels above the clicked spot displaying its formatted degrees, minutes and seconds (such as 50° 03′ 41″ N 19° 56′ 12″ E) alongside a × close button.

Clicking near the edge of the viewport smoothly pans the camera over 250 milliseconds until the entire popup sits comfortably 20 pixels inside the frame, and subsequent clicks relocate the existing overlay.

When you need it

Virtually every consumer and enterprise map answers a tap or click on a location with a contextual card showing coordinates, street addresses, parcel summaries or action links.

Enabling OpenLayers’ built-in autoPan prevents tall or wide cards clicked near screen edges from clipping off-screen.

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.

popup.ts
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { toStringHDMS } from 'ol/coordinate';
import type MapBrowserEvent from 'ol/MapBrowserEvent';
import { toLonLat } from 'ol/proj';
import {
  ContentComponent,
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerTileComponent,
  MapComponent,
  OverlayComponent,
  SourceOsmComponent,
  ViewComponent,
} from 'cc-openlayers';

interface Clicked {
  coordinate: number[];
  hdms: string;
}

/** A popup anchored to the clicked place by an overlay, closed with its button. */
@Component({
  selector: 'app-popup',
  imports: [
    ContentComponent,
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerTileComponent,
    MapComponent,
    OverlayComponent,
    SourceOsmComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px" (singleClick)="open($event)">
      <aol-interaction-default />
      <aol-control-defaults />
      <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>
      @if (clicked(); as place) {
        <aol-overlay
          positioning="bottom-center"
          [offset]="[0, -8]"
          [autoPan]="true"
          [autoPanAnimation]="{ duration: 250 }"
        >
          <aol-coordinate [x]="place.coordinate[0]" [y]="place.coordinate[1]" />
          <aol-content>
            <div class="example-label">
              <p>You clicked here:</p>
              <code>{{ place.hdms }}</code>
              <button type="button" aria-label="Close the popup" (click)="clicked.set(undefined)">×</button>
            </div>
          </aol-content>
        </aol-overlay>
      }
    </aol-map>
    <div class="example-controls">
      <p class="example-hint">Click the map to get a popup.</p>
    </div>
  `,
})
export class Popup {
  protected readonly clicked = signal<Clicked | undefined>(undefined);

  protected open(event: MapBrowserEvent): void {
    this.clicked.set({ coordinate: event.coordinate, hdms: toStringHDMS(toLonLat(event.coordinate)) });
  }
}

Architecture and OpenLayers mechanics

(singleClick) invokes open, storing the projected coordinate and its toStringHDMS(toLonLat(event.coordinate)) text inside the clicked signal. Conditional @if (clicked(); as place) mounts aol-overlay on first click, moves that same overlay on subsequent clicks, and unmounts it when × resets clicked to undefined.

positioning="bottom-center" with [offset]="[0, -8]" places the card’s bottom midpoint just above the click, while [autoPan]="true" and [autoPanAnimation]="{ duration: 250 }" nudge the camera into view.

How it works

  • (singleClick) formats the clicked coordinate via toStringHDMS(toLonLat(event.coordinate)) into the reactive clicked signal.
  • @if (clicked(); as place) mounts the overlay on first click, repositions it on later clicks and removes it when clicked becomes undefined.
  • positioning="bottom-center" and [offset]="[0, -8]" float the popup 8 pixels above the clicked coordinate.
  • [autoPan]="true" with [autoPanAnimation]="{ duration: 250 }" scrolls the map smoothly whenever a popup would overflow the viewport edge.

Pitfalls

  • OpenLayers’ singleClick intentionally waits 250 ms to verify the user is not performing a double-click zoom; listening to raw click instead responds immediately but also fires twice during every double-click zoom.
  • Keep stopEvent at its default true on interactive popups—setting it to false lets clicks on the × button or text selection pass through to the map and re-open the popup underneath.
  • With one argument, toStringHDMS outputs latitude before longitude rounded to whole arc-seconds (~31 m N–S and ~20 m E–W in Kraków); pass a fractional digits argument when sub-metre precision is needed.

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.