Essentials · Angular, React and OpenLayers

A rotated map view

Initialize a rotated OpenLayers map view in radians, rotate interactively with Alt+Shift drag or touch gestures, and snap back north with controls.

A street map of Kraków and its surroundings at zoom 10, rotated by 30 degrees with its labels, and the rotate button in the top right corner.
A screenshot of the example. Map data © OpenStreetMap contributors.

What this example shows

The street map of Kraków and its metropolitan surroundings opens at zoom 10 tilted 30 degrees clockwise from north, automatically revealing OpenLayers’ compass reset button in the top-right corner. Desktop users can rotate further with Alt+Shift and mouse drag, touch users can twist with two fingers, and clicking the compass animates smoothly back to north-up.

Releasing a manual rotation within a few degrees of true north snaps the heading cleanly back to zero so casual zoom gestures never leave the map slightly crooked.

When you need it

Strict north-up orientation is often awkward for architectural site plans aligned with a building facade, river-corridor inspections, runway diagrams or turn-by-turn navigation screens.

Rotating the OpenLayers view transforms canvas rendering on the fly without mutating underlying vector geometries or reprojecting coordinate data, letting users switch effortlessly between project-aligned headings and standard north-up views.

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.

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

/** A map whose view starts rotated by 30 degrees; Alt+Shift+drag rotates it further. */
@Component({
  selector: 'app-rotation',
  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]="10" [rotation]="rotation">
        <aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" />
      </aol-view>
      <aol-layer-tile><aol-source-osm /></aol-layer-tile>
    </aol-map>
    <p class="example-hint">Use Alt+Shift+drag to rotate the map.</p>
  `,
})
export class Rotation {
  protected readonly rotation = Math.PI / 6;
}

Architecture and OpenLayers mechanics

OpenLayers expresses view [rotation]="rotation" in clockwise radians with 0 pointing due north, so a 30-degree clockwise angle is configured as Math.PI / 6 (~0.5236 rad). Standard default interactions already include Alt+Shift mouse rotation and two-finger touch pinch-rotate, while default controls mount an auto-hiding compass button whenever rotation is non-zero.

Built-in view rotation constraints gently snap near-zero angles back to exact north at the end of a gesture.

How it works

  • [rotation]="rotation" initialises the camera heading to Math.PI / 6 radians (30 degrees clockwise from north).
  • Default map interactions handle both Alt+Shift desktop drag rotation and two-finger touch rotation without extra configuration.
  • The default rotate control stays hidden at 0 radians and appears automatically whenever the map is tilted, resetting north on click.
  • Default rotation constraints snap small residual angles near north back to exact zero when a gesture ends.

Pitfalls

  • Never pass degrees where OpenLayers expects radians: writing 30 instead of Math.PI / 6 spins the view nearly five full turns (~279°); always convert degrees via degrees * Math.PI / 180.
  • Labels baked into raster PNG tiles rotate upside-down past 90°, whereas OpenLayers vector text and icon styles remain upright unless explicitly configured to rotate with the view.
  • Axis-aligned bounding boxes calculated from a rotated view cover a larger geographic envelope than the tilted viewport window.

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.