Controls · Angular, React and OpenLayers

A graticule with labels

Overlay a toggleable OpenLayers coordinate graticule with degree-and-minute labels and custom dotted orange strokes over a Web Mercator map.

A map of Kraków and its surroundings with an orange dotted graticule every ten minutes, labelled in degrees and minutes, and the Graticule checkbox below it.
A screenshot of the example. Map data © OpenStreetMap contributors.

What this example shows

Over Kraków and its surrounding Małopolska towns, a dotted orange latitude-and-longitude grid (aol-graticule) automatically subdivides into clean 10-minute intervals with degree-and-minute coordinate labels along the viewport edges.

Zooming in or out adapts meridian and parallel spacing dynamically to the current scale, and a checkbox beneath the map mounts or removes the entire graticule layer cleanly.

When you need it

Topographic reference maps, maritime and aviation planning screens, surveying tools and print layouts require a labelled geographic coordinate grid so readers can estimate longitude and latitude visually across the whole canvas.

OpenLayers’ Graticule layer generates and labels meridians and parallels dynamically on the client with zero external vector datasets or tile servers.

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.

graticule.ts
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import Stroke from 'ol/style/Stroke';
import {
  ControlFullScreenComponent,
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerTileComponent,
  MapComponent,
  SourceOsmComponent,
  ViewComponent,
} from 'cc-openlayers';
import { GraticuleComponent } from 'cc-openlayers/graticule';

/** A labelled graticule of meridians and parallels drawn over the map, shown and hidden with a checkbox. */
@Component({
  selector: 'app-graticule',
  imports: [
    ControlFullScreenComponent,
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    GraticuleComponent,
    LayerTileComponent,
    MapComponent,
    SourceOsmComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px">
      <aol-interaction-default />
      <aol-control-defaults />
      <aol-control-fullscreen />
      <aol-view [zoom]="10"><aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" /></aol-view>
      <aol-layer-tile><aol-source-osm /></aol-layer-tile>
      @if (shown()) {
        <aol-graticule [strokeStyle]="stroke" [showLabels]="true" />
      }
    </aol-map>
    <div class="example-controls">
      <label><input type="checkbox" [checked]="shown()" (change)="toggle($event)" /> Graticule</label>
    </div>
  `,
})
export class Graticule {
  readonly shown = signal(true);
  protected readonly stroke = new Stroke({ color: 'rgba(255,120,0,0.9)', width: 2, lineDash: [0.5, 4] });

  protected toggle(event: Event): void {
    this.shown.set((event.target as HTMLInputElement).checked);
  }
}

Architecture and OpenLayers mechanics

aol-graticule from cc-openlayers/graticule mounts OpenLayers’ Graticule layer with [showLabels] enabled so edge ticks display formatted degrees and minutes. Passing a custom OpenLayers Stroke to [strokeStyle]—2 pixels wide in orange with a lineDash of half-pixel dashes, four-pixel gaps and round caps—renders crisp circular dots along every meridian and parallel.

Wrapping the element in @if bound to a checkbox signal attaches and disposes of the graticule layer cleanly on demand.

How it works

  • aol-graticule from cc-openlayers/graticule draws a dynamic longitude/latitude grid with edge coordinate labels enabled via [showLabels].
  • [strokeStyle] applies a 2-pixel orange OpenLayers Stroke whose lineDash and round line caps turn short dashes into crisp dots.
  • A signal-driven @if block adds and removes the graticule layer reactively when the checkbox toggles.

Pitfalls

  • Edge degree labels compete for corner space on very compact maps—verify label placement against zoom controls and attribution bars on narrow mobile viewports.
  • On a Web Mercator map, parallels of latitude spread progressively farther apart toward the poles; that visual spacing reflects conformal Mercator math rather than uneven graticule intervals.

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.