Essentials · Angular, React and OpenLayers

The smallest complete map

Create the smallest complete OpenLayers map in Angular and React with a sized container, Web Mercator view, OpenStreetMap tiles, controls and gestures.

A street map of central Kraków at zoom 13 with the zoom buttons in the top left corner and the OpenStreetMap credit in the bottom right.
A screenshot of the example. Map data © OpenStreetMap contributors.

What this example shows

This example distils an OpenLayers integration down to the minimal self-contained markup needed for a fully interactive street map. Opening at zoom level 13 over central Kraków—framing the Old Town Planty ring, Wawel bend of the Vistula and neighbouring districts—it renders top-left zoom buttons, auto-hiding rotation reset and bottom-right OpenStreetMap attribution.

The component class itself holds zero state, event handlers or lifecycle hooks: every piece of map behaviour comes directly from explicit elements in the template.

When you need it

Context maps that simply present a fixed location—such as a company office on a contact page, an event venue card or a clean baseline to copy when starting a new map feature—need no external state management.

Keeping a minimal reference template is also the fastest way to verify bundler styles, SSR/prerender compatibility and container layout sizing before adding dynamic vector layers, projections or custom pointer tools.

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.

simple.ts
import { ChangeDetectionStrategy, Component } from '@angular/core';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerTileComponent,
  MapComponent,
  SourceOsmComponent,
  ViewComponent,
} from 'cc-openlayers';

/** A simple map with an OSM source. */
@Component({
  selector: 'app-simple',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerTileComponent,
    MapComponent,
    SourceOsmComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px">
      <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>
    </aol-map>
  `,
})
export class Simple {}

Architecture and OpenLayers mechanics

Whereas OpenLayers’ imperative map constructor silently attaches default pan/zoom interactions and default controls behind the scenes, the declarative wrapper uses strict opt-in composition. aol-map owns the DOM viewport, aol-view sets scale and centre, aol-layer-tile pairs with aol-source-osm for raster tiles, aol-interaction-default enables mouse, wheel and touch navigation, and aol-control-defaults mounts zoom, rotate and attribution widgets.

At zoom 13, Web Mercator ground resolution is roughly 19 metres per pixel at the equator and 12 metres per pixel at Kraków’s 50° N latitude—ideal for city-centre legibility on initial load.

How it works

  • aol-map, aol-view, aol-layer-tile and aol-source-osm assemble the map container, view, raster layer and OpenStreetMap source with an empty component class.
  • Interactions and widgets are opted into explicitly: without aol-interaction-default the map cannot be panned or zoomed, and without aol-control-defaults zoom buttons and data credits are omitted.
  • The view runs in Web Mercator to match standard XYZ tiles while aol-coordinate with srid="EPSG:4326" specifies the Kraków centre in decimal degrees.
  • height="480px" gives the host element a concrete pixel height before OpenLayers measures its canvas.

Pitfalls

  • When porting imperative OpenLayers snippets, remember that the component library adds nothing behind your back: include both aol-interaction-default and aol-control-defaults whenever standard navigation and attribution are required.
  • Public OpenStreetMap tile servers require visible credit on the map and permit only light traffic; production apps with commercial or heavy usage must switch to a dedicated self-hosted or commercial tile service.
  • Always load OpenLayers’ base CSS in global application styles so zoom buttons and collapsible attributions lay out properly inside the viewport corners.

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.