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.

A street map of Kraków's Old Town with a green point on the Main Square and a pin at Wawel, and sliders for zoom, longitude, latitude and opacity below it.
A screenshot of the example. Map data © OpenStreetMap contributors.

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.ts
import { 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;
  }
}

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-view and aol-layer-tile instantiate 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-coordinate inputs and both layers’ opacity properties 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 moveend when application state must also follow user gestures, as shown in the map-position guide.
  • Mutate scalar bindings such as opacity and zoom rather than tearing down and recreating layer elements on state updates, keeping tile caches warm and flicker-free.

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.