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.

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.tsimport { 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;
} 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.
rotation.react.tsximport { DefaultControls, DefaultInteractions, Coordinate, Map, OSMSource, TileLayer, View } from 'cc-openlayers/react';
const rotation = Math.PI / 6;
/** A map whose view starts rotated by 30 degrees; Alt+Shift+drag rotates it further. */
export function Rotation() {
return (
<>
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
<View zoom={10} rotation={rotation}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
</Map>
<p className="example-hint">Use Alt+Shift+drag to rotate the map.</p>
</>
);
}How the React version works
The view starts rotated by 30° (π/6); Alt+Shift and a drag rotate it further.
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.
From Angular to React
| Angular | React |
|---|---|
[rotation]="rotation" from a class field | rotation={rotation} from a module constant |
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.
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 toMath.PI / 6radians (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 / 6spins 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.
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.