Essentials · Angular, React and OpenLayers
A map in an Angular component
Build a reactive OpenLayers map component in Angular and React: bind zoom, WGS84 center coordinates, layer opacity and vector markers with signals.

What this example shows
An interactive street map opens over Kraków’s Old Town inside a 480-pixel viewport, combining an OpenStreetMap base layer with a vector overlay marking two landmarks: a green circle on the Main Square and an SVG pin on Wawel Hill. Directly beneath the map, four range sliders let you adjust zoom level, longitude, latitude and layer transparency live.
Dragging any control updates reactive component state first, and the declarative template bindings push those changes into the underlying OpenLayers view and layers without recreating the map canvas.
When you need it
Every interactive WebGIS screen starts here: a map whose camera position, layer stack and feature overlays are driven declaratively by component state rather than imperative DOM glue code.
In dashboards, parcel viewers, fleet monitors and asset inspectors, surrounding UI panels constantly adjust what the map displays—toggling layer visibility, fading a raster backdrop or jumping to a district. Declaring the map hierarchy in markup and driving it via Angular signals or React props aligns creation, updates and disposal with the component lifecycle.
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.
basic.tsimport { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import {
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
FeatureComponent,
GeometryPointComponent,
LayerTileComponent,
LayerVectorComponent,
MapComponent,
SourceOsmComponent,
SourceVectorComponent,
StyleCircleComponent,
StyleComponent,
StyleFillComponent,
StyleIconComponent,
StyleStrokeComponent,
ViewComponent,
} from 'cc-openlayers';
/** A map that follows Angular state: the view's zoom and centre and the layers' opacity are signals. */
@Component({
selector: 'app-basic',
imports: [
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
FeatureComponent,
GeometryPointComponent,
LayerTileComponent,
LayerVectorComponent,
MapComponent,
SourceOsmComponent,
SourceVectorComponent,
StyleCircleComponent,
StyleComponent,
StyleFillComponent,
StyleIconComponent,
StyleStrokeComponent,
ViewComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<aol-map height="480px">
<aol-interaction-default />
<aol-control-defaults />
<aol-view [zoom]="zoom()">
<aol-coordinate [x]="lon()" [y]="lat()" srid="EPSG:4326" />
</aol-view>
<aol-layer-tile [opacity]="opacity()"><aol-source-osm /></aol-layer-tile>
<aol-layer-vector [opacity]="opacity()">
<aol-source-vector>
<aol-feature>
<aol-geometry-point><aol-coordinate [x]="19.9373" [y]="50.0617" srid="EPSG:4326" /></aol-geometry-point>
<aol-style>
<aol-style-circle [radius]="10">
<aol-style-stroke color="black" [width]="5" />
<aol-style-fill color="green" />
</aol-style-circle>
</aol-style>
</aol-feature>
<aol-feature>
<aol-geometry-point><aol-coordinate [x]="19.9356" [y]="50.054" srid="EPSG:4326" /></aol-geometry-point>
<aol-style>
<aol-style-icon src="assets/marker.svg" [anchor]="[0.5, 1]" [scale]="2" />
</aol-style>
</aol-feature>
</aol-source-vector>
</aol-layer-vector>
</aol-map>
<div class="example-controls">
<label>
Zoom <input type="range" min="1" max="18" [value]="zoom()" (input)="zoom.set(number($event))" />
<output>{{ zoom() }}</output>
</label>
<label>
Longitude
<input type="range" min="19.8" max="20.1" step="0.001" [value]="lon()" (input)="lon.set(number($event))" />
<output>{{ lon() }}</output>
</label>
<label>
Latitude
<input type="range" min="49.97" max="50.13" step="0.001" [value]="lat()" (input)="lat.set(number($event))" />
<output>{{ lat() }}</output>
</label>
<label>
Opacity
<input type="range" min="0" max="1" step="0.1" [value]="opacity()" (input)="opacity.set(number($event))" />
<output>{{ opacity() }}</output>
</label>
</div>
`,
})
export class Basic {
readonly zoom = signal(15);
readonly lon = signal(19.9368);
readonly lat = signal(50.0614);
readonly opacity = signal(1);
protected number(event: Event): number {
return (event.target as HTMLInputElement).valueAsNumber;
}
} 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.
basic.react.tsximport { useState, type ChangeEvent } from 'react';
import {
Coordinate,
DefaultControls,
DefaultInteractions,
Feature,
IconStyle,
CircleStyle,
Fill,
Map,
OSMSource,
PointGeometry,
Stroke,
Style,
TileLayer,
VectorLayer,
VectorSource,
View,
} from 'cc-openlayers/react';
const anchor = [0.5, 1];
function number(event: ChangeEvent<HTMLInputElement>): number {
return event.target.valueAsNumber;
}
/** A map that follows React state: the view's zoom and centre and the layers' opacity are state. */
export function Basic() {
const [zoom, setZoom] = useState(15);
const [lon, setLon] = useState(19.9368);
const [lat, setLat] = useState(50.0614);
const [opacity, setOpacity] = useState(1);
return (
<>
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
<View zoom={zoom}>
<Coordinate x={lon} y={lat} srid="EPSG:4326" />
</View>
<TileLayer opacity={opacity}>
<OSMSource />
</TileLayer>
<VectorLayer opacity={opacity}>
<VectorSource>
<Feature>
<PointGeometry>
<Coordinate x={19.9373} y={50.0617} srid="EPSG:4326" />
</PointGeometry>
<Style>
<CircleStyle radius={10}>
<Stroke color="black" width={5} />
<Fill color="green" />
</CircleStyle>
</Style>
</Feature>
<Feature>
<PointGeometry>
<Coordinate x={19.9356} y={50.054} srid="EPSG:4326" />
</PointGeometry>
<Style>
<IconStyle src="assets/marker.svg" anchor={anchor} scale={2} />
</Style>
</Feature>
</VectorSource>
</VectorLayer>
</Map>
<div className="example-controls">
<label>
Zoom <input type="range" min="1" max="18" value={zoom} onChange={(event) => setZoom(number(event))} />
<output>{zoom}</output>
</label>
<label>
Longitude
<input
type="range"
min="19.8"
max="20.1"
step="0.001"
value={lon}
onChange={(event) => setLon(number(event))}
/>
<output>{lon}</output>
</label>
<label>
Latitude
<input
type="range"
min="49.97"
max="50.13"
step="0.001"
value={lat}
onChange={(event) => setLat(number(event))}
/>
<output>{lat}</output>
</label>
<label>
Opacity
<input
type="range"
min="0"
max="1"
step="0.1"
value={opacity}
onChange={(event) => setOpacity(number(event))}
/>
<output>{opacity}</output>
</label>
</div>
</>
);
}How the React version works
The zoom, the view's centre and the opacity of both layers come from the component's state, and the sliders below the map change it. The map holds two points: a circle and an icon. The adapter's props work like the aol-* inputs: a changed value updates the existing OpenLayers object instead of creating a new one.
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. Both adapters build the point styles from ol/style in the same way (style, circle, stroke, fill, icon).
From Angular to React
| Angular | React |
|---|---|
signal(15) and zoom.set(...) | useState(15) and setZoom(...) |
[zoom]="zoom()", [opacity]="opacity()" | zoom={zoom}, opacity={opacity} |
(input)="zoom.set(number($event))" | onChange={(event) => setZoom(number(event))} (React's onChange on a range input fires like the input event) |
{{ zoom() }} in <output> | {zoom} in <output> |
[anchor]="[0.5, 1]" | the module constant anchor passed as anchor={anchor} |
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. A change of state renders the component again; the adapter compares the props and calls only the matching setter (setZoom, setOpacity, setCenter through Coordinate). The anchor array is a module constant, because a new array on every render would be a new value to the adapter.
Architecture and OpenLayers mechanics
OpenLayers cleanly separates the viewport and render loop (aol-map) from camera geometry (aol-view) and data rendering (aol-layer-tile and vector layers). Although OpenStreetMap tiles are served in Spherical Mercator metres, user interfaces almost always hold coordinates in decimal degrees; setting srid="EPSG:4326" on aol-coordinate converts geographic degrees into the view’s native Web Mercator projection automatically whenever inputs change.
Vector markers follow the same compositional hierarchy: point features sit inside a vector source under a vector layer, each paired with declarative circle or icon style components that dispose cleanly when the view unmounts.
How it works
aol-map,aol-viewandaol-layer-tileinstantiate the OpenLayers map, camera view and tile layer in template order and dispose of them automatically with the host component.- Camera zoom, the centre’s
aol-coordinateinputs and both layers’opacityproperties bind directly to reactive signals or props so slider edits mutate only the affected OpenLayers property. srid="EPSG:4326"lets component state express the map centre in readable WGS84 degrees while the view itself stays in Web Mercator.- Two point features—a styled circle marker on the Main Square and an anchored SVG pin at Wawel—are declared directly in markup alongside the base layer.
- Default pan/zoom gestures and on-map controls are mounted explicitly as child components so the map carries only the capabilities declared in its template.
Pitfalls
- An OpenLayers container has zero intrinsic height: always set an explicit size such as
height="480px"or a CSS layout height, otherwise the canvas initialises at 0 pixels and shows nothing. - Template bindings here flow one way from UI state to the map: dragging the map with the mouse moves the internal view without updating your signals; listen to
moveendwhen application state must also follow user gestures, as shown in the map-position guide. - Mutate scalar bindings such as
opacityand zoom rather than tearing down and recreating layer elements on state updates, keeping tile caches warm and flicker-free.
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.