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.

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.tsimport { 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)) });
}
} 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.
popup.react.tsximport { useState } from 'react';
import { toStringHDMS } from 'ol/coordinate.js';
import type MapBrowserEvent from 'ol/MapBrowserEvent.js';
import { toLonLat } from 'ol/proj.js';
import {
Coordinate,
DefaultControls,
DefaultInteractions,
Map,
OSMSource,
Overlay,
TileLayer,
View,
} from 'cc-openlayers/react';
interface Clicked {
coordinate: number[];
hdms: string;
}
const offset = [0, -8];
const autoPan = { animation: { duration: 250 } };
/** A popup anchored to the clicked place by an overlay, closed with its button. */
export function Popup() {
const [clicked, setClicked] = useState<Clicked | undefined>(undefined);
const open = (event: MapBrowserEvent) => {
setClicked({ coordinate: event.coordinate, hdms: toStringHDMS(toLonLat(event.coordinate)) });
};
return (
<>
<Map height="480px" onSingleClick={open}>
<DefaultInteractions />
<DefaultControls />
<View zoom={13}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
{clicked ? (
<Overlay positioning="bottom-center" offset={offset} autoPan={autoPan}>
<Coordinate x={clicked.coordinate[0]} y={clicked.coordinate[1]} />
<div className="example-label">
<p>You clicked here:</p>
<code>{clicked.hdms}</code>
<button type="button" aria-label="Close the popup" onClick={() => setClicked(undefined)}>
×
</button>
</div>
</Overlay>
) : null}
</Map>
<div className="example-controls">
<p className="example-hint">Click the map to get a popup.</p>
</div>
</>
);
}How the React version works
A click on the map opens a popup above that place with its coordinates in degrees, minutes and seconds; the “×” button closes it. The overlay pans the map to keep the popup visible (auto-pan with a 250 ms animation).
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. OpenLayers' toStringHDMS formats the coordinates.
From Angular to React
| Angular | React |
|---|---|
(singleClick)="open($event)" | onSingleClick={open} |
@if (clicked()) around aol-overlay | {clicked ? <Overlay …> : null} |
[autoPan]="true", [autoPanAnimation]="{ duration: 250 }" | autoPan={{ animation: { duration: 250 } }} as a module constant (the OpenLayers option the Angular component builds from two inputs) |
(click)="clicked.set(undefined)" | onClick={() => setClicked(undefined)} |
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 overlay exists only while the popup is open: closing it unmounts Overlay, which removes itself from the map.
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 viatoStringHDMS(toLonLat(event.coordinate))into the reactiveclickedsignal.@if (clicked(); as place)mounts the overlay on first click, repositions it on later clicks and removes it whenclickedbecomesundefined.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’
singleClickintentionally waits 250 ms to verify the user is not performing a double-click zoom; listening to rawclickinstead responds immediately but also fires twice during every double-click zoom. - Keep
stopEventat its defaulttrueon 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,
toStringHDMSoutputs 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.
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.