Projections · Angular, React and OpenLayers
Switch the view projection
Switch an OpenLayers map View dynamically between EPSG:3857 Web Mercator and EPSG:4326 geographic degrees using signals and resolution events.

What this example shows
Over Kraków with point markers in the Old Town and Nowa Huta, a projection dropdown below the viewport switches the live map dynamically between EPSG:3857 (Spherical Mercator metres) and EPSG:4326 (geographic degrees), accompanied by a Zoom in button and a live counter of view resolution changes.
When you need it
Multi-purpose geoportals and educational cartography tools sometimes let users toggle between conformal Web Mercator browsing and unprojected EPSG:4326 degree views while keeping plotted markers in place.
Binding [projection] on aol-view to a reactive signal handles View replacement, centre preservation and vector feature re-transformation declaratively.
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.
view-projection-update.tsimport { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import {
CoordinateComponent,
DefaultInteractionComponent,
FeatureComponent,
GeometryPointComponent,
LayerTileComponent,
LayerVectorComponent,
MapComponent,
SourceOsmComponent,
SourceVectorComponent,
StyleCircleComponent,
StyleComponent,
StyleFillComponent,
StyleIconComponent,
StyleStrokeComponent,
ViewComponent,
} from 'cc-openlayers';
/** The view's projection switched at run time between Web Mercator and geographic coordinates. */
@Component({
selector: 'app-view-projection-update',
imports: [
CoordinateComponent,
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-view
[zoom]="zoom()"
[projection]="projection()"
(changeResolution)="resolutionChanges.update((count) => count + 1)"
>
<aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" />
</aol-view>
<aol-layer-tile><aol-source-osm /></aol-layer-tile>
<aol-layer-vector>
<aol-source-vector>
<aol-feature>
<aol-geometry-point><aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" /></aol-geometry-point>
<aol-style>
<aol-style-circle [radius]="10">
<aol-style-stroke color="black" [width]="2" />
<aol-style-fill color="green" />
</aol-style-circle>
</aol-style>
</aol-feature>
<aol-feature>
<aol-geometry-point><aol-coordinate [x]="20.0373" [y]="50.0718" 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>
Projection
<select (change)="selectProjection($event)">
<option value="EPSG:3857">Web Mercator (EPSG:3857)</option>
<option value="EPSG:4326">Geographic (EPSG:4326)</option>
</select>
</label>
<label>
Active projection
<output>{{ projection() }}</output>
</label>
<button type="button" (click)="zoom.update((zoom) => zoom + 1)">Zoom in</button>
<label>
Resolution changes
<output>{{ resolutionChanges() }}</output>
</label>
</div>
`,
})
export class ViewProjectionUpdate {
readonly projection = signal('EPSG:3857');
readonly zoom = signal(12);
readonly resolutionChanges = signal(0);
protected selectProjection(event: Event): void {
this.projection.set((event.target as HTMLSelectElement).value);
}
} 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.
view-projection-update.react.tsximport { useState } from 'react';
import {
CircleStyle,
Coordinate,
DefaultInteractions,
Feature,
Fill,
IconStyle,
Map,
OSMSource,
PointGeometry,
Stroke,
Style,
TileLayer,
VectorLayer,
VectorSource,
View,
} from 'cc-openlayers/react';
const ANCHOR = [0.5, 1];
/** The view's projection switched at run time between Web Mercator and geographic coordinates. */
export function ViewProjectionUpdate() {
const [projection, setProjection] = useState('EPSG:3857');
const [zoom, setZoom] = useState(12);
const [resolutionChanges, setResolutionChanges] = useState(0);
return (
<>
<Map height="480px">
<DefaultInteractions />
<View zoom={zoom} projection={projection} onChangeResolution={() => setResolutionChanges((count) => count + 1)}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
<VectorLayer>
<VectorSource>
<Feature>
<PointGeometry>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</PointGeometry>
<Style>
<CircleStyle radius={10}>
<Stroke color="black" width={2} />
<Fill color="green" />
</CircleStyle>
</Style>
</Feature>
<Feature>
<PointGeometry>
<Coordinate x={20.0373} y={50.0718} srid="EPSG:4326" />
</PointGeometry>
<Style>
<IconStyle src="assets/marker.svg" anchor={ANCHOR} scale={2} />
</Style>
</Feature>
</VectorSource>
</VectorLayer>
</Map>
<div className="example-controls">
<label>
Projection
<select value={projection} onChange={(event) => setProjection(event.target.value)}>
<option value="EPSG:3857">Web Mercator (EPSG:3857)</option>
<option value="EPSG:4326">Geographic (EPSG:4326)</option>
</select>
</label>
<label>
Active projection
<output>{projection}</output>
</label>
<button type="button" onClick={() => setZoom((current) => current + 1)}>
Zoom in
</button>
<label>
Resolution changes
<output>{resolutionChanges}</output>
</label>
</div>
</>
);
}How the React version works
A map whose view switches between Web Mercator and geographic coordinates from a list, with a zoom button and a count of the view's resolution changes, over two point features.
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. The example has no shared file.
From Angular to React
| Angular | React |
|---|---|
signals of the projection, the zoom and the count | useState |
<aol-view [zoom]="zoom()" [projection]="projection()" (changeResolution)="…"> | <View zoom={zoom} projection={projection} onChangeResolution={…}> |
<select (change)="selectProjection($event)"> | <select value={projection} onChange={…}> |
(click)="zoom.update(…)" | onClick={() => setZoom(…)} |
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
When the signal bound to [projection] on aol-view changes between EPSG:3857 and EPSG:4326, the wrapper constructs a new OpenLayers View in the target CRS, transforms the current centre coordinate into it, and re-projects declarative features (srid="EPSG:4326").
(changeResolution) increments a reactive counter whenever zooming or view updates alter pixel resolution.
How it works
- Binding
[projection]onaol-viewto a signal replaces the underlying OpenLayers View cleanly while preserving the map centre. - Point features declared with
srid="EPSG:4326"automatically project into whichever View CRS is currently active. (changeResolution)tracks resolution changes triggered by zooming in or out.- Reading the active projection code from the same signal keeps UI readouts synchronized with the canvas.
Pitfalls
- Switching to
EPSG:4326triggers client-side triangle warping of Web Mercator OpenStreetMap tiles, which slightly softens raster text compared with nativeEPSG:3857rendering. - Identical integer zoom numbers represent different physical ground scales in
EPSG:3857versusEPSG:4326—use getPointResolution (projection-and-scale) when ground scale must stay constant across switches.
Related examples
Background: state and lifecycle of maps in Angular with OpenLayers, which the components of cc-openlayers take care of.