Projections · Angular, React and OpenLayers
Keep the scale when the projection changes
Preserve true ground scale at the map centre when switching OpenLayers views between WGS84 and Web Mercator using getPointResolution ratios.

What this example shows
Centred on Poland with a four-step 1 : N cartographic scale bar in the bottom-left corner, switching the View projection dropdown between EPSG:3857 (Web Mercator) and EPSG:4326 (WGS 84) keeps both the geographic centre and the ground coverage per pixel consistent across the transition.
When you need it
Naively copying view.getZoom() or raw view.getResolution() when switching between a metre-based projection and a degree-based projection causes jarring scale jumps.
Converting resolution through OpenLayers’ getPointResolution preserves the user’s visual working scale across any pair of coordinate systems.
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.
projection-and-scale.shared.tsimport { getPointResolution, get as getProjection, transform } from 'ol/proj.js';
import type View from 'ol/View.js';
/**
* The resolution a view in the projection of the code should have to keep the scale of the given view (measured at its
* centre); undefined when the code is unknown or the view has no centre or resolution yet.
*/
export function resolutionInProjection(view: View, code: string): number | undefined {
const current = view.getProjection();
const next = getProjection(code);
const center = view.getCenter();
const resolution = view.getResolution();
if (!next || !center || resolution === undefined) return undefined;
const newCenter = transform(center, current, next);
const currentMetersPerUnit = current.getMetersPerUnit() ?? 1;
const nextMetersPerUnit = next.getMetersPerUnit() ?? 1;
const currentPointResolution =
getPointResolution(current, 1 / currentMetersPerUnit, center, 'm') * currentMetersPerUnit;
const nextPointResolution = getPointResolution(next, 1 / nextMetersPerUnit, newCenter, 'm') * nextMetersPerUnit;
return (resolution * currentPointResolution) / nextPointResolution;
}projection-and-scale.tsimport { ChangeDetectionStrategy, Component, signal, viewChild } from '@angular/core';
import {
ControlScaleLineComponent,
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerTileComponent,
MapComponent,
SourceOsmComponent,
ViewComponent,
} from 'cc-openlayers';
import { resolutionInProjection } from './projection-and-scale.shared';
/** A scale bar that keeps its scale when the view changes between Web Mercator and geographic coordinates. */
@Component({
selector: 'app-projection-and-scale',
imports: [
ControlScaleLineComponent,
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerTileComponent,
MapComponent,
SourceOsmComponent,
ViewComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<aol-map height="480px">
<aol-interaction-default />
<aol-control-defaults />
<aol-control-scaleline units="metric" [bar]="true" [steps]="4" [text]="true" [minWidth]="140" />
<aol-view [zoom]="6" [projection]="projection()" [resolution]="resolution()">
<aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" />
</aol-view>
<aol-layer-tile><aol-source-osm /></aol-layer-tile>
</aol-map>
<div class="example-controls">
<label>
View projection
<select (change)="changeProjection($event)">
<option value="EPSG:3857" [selected]="projection() === 'EPSG:3857'">Spherical Mercator (EPSG:3857)</option>
<option value="EPSG:4326" [selected]="projection() === 'EPSG:4326'">WGS 84 (EPSG:4326)</option>
</select>
</label>
</div>
`,
})
export class ProjectionAndScale {
readonly projection = signal('EPSG:4326');
/** Set when the projection changes, so the new view keeps the scale of the old one; the initial zoom decides before. */
readonly resolution = signal<number | undefined>(undefined);
private readonly map = viewChild.required(MapComponent);
protected changeProjection(event: Event): void {
const code = (event.target as HTMLSelectElement).value;
const resolution = resolutionInProjection(this.map().instance.getView(), code);
if (resolution === undefined) return;
this.resolution.set(resolution);
this.projection.set(code);
}
} 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.
projection-and-scale.react.tsximport { useRef, useState } from 'react';
import type OLMap from 'ol/Map.js';
import {
Coordinate,
DefaultControls,
DefaultInteractions,
Map,
OSMSource,
ScaleLineControl,
TileLayer,
View,
} from 'cc-openlayers/react';
import { resolutionInProjection } from './projection-and-scale.shared';
/** A scale bar that keeps its scale when the view changes between Web Mercator and geographic coordinates. */
export function ProjectionAndScale() {
const [projection, setProjection] = useState('EPSG:4326');
/** Set when the projection changes, so the new view keeps the scale of the old one; the initial zoom decides before. */
const [resolution, setResolution] = useState<number | undefined>(undefined);
const map = useRef<OLMap>(null);
const changeProjection = (code: string) => {
if (!map.current) return;
const next = resolutionInProjection(map.current.getView(), code);
if (next === undefined) return;
setResolution(next);
setProjection(code);
};
return (
<>
<Map ref={map} height="480px">
<DefaultInteractions />
<DefaultControls />
<ScaleLineControl units="metric" bar={true} steps={4} text={true} minWidth={140} />
<View zoom={6} projection={projection} resolution={resolution}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
</Map>
<div className="example-controls">
<label>
View projection
<select value={projection} onChange={(event) => changeProjection(event.target.value)}>
<option value="EPSG:3857">Spherical Mercator (EPSG:3857)</option>
<option value="EPSG:4326">WGS 84 (EPSG:4326)</option>
</select>
</label>
</div>
</>
);
}projection-and-scale.shared.tsimport { getPointResolution, get as getProjection, transform } from 'ol/proj.js';
import type View from 'ol/View.js';
/**
* The resolution a view in the projection of the code should have to keep the scale of the given view (measured at its
* centre); undefined when the code is unknown or the view has no centre or resolution yet.
*/
export function resolutionInProjection(view: View, code: string): number | undefined {
const current = view.getProjection();
const next = getProjection(code);
const center = view.getCenter();
const resolution = view.getResolution();
if (!next || !center || resolution === undefined) return undefined;
const newCenter = transform(center, current, next);
const currentMetersPerUnit = current.getMetersPerUnit() ?? 1;
const nextMetersPerUnit = next.getMetersPerUnit() ?? 1;
const currentPointResolution =
getPointResolution(current, 1 / currentMetersPerUnit, center, 'm') * currentMetersPerUnit;
const nextPointResolution = getPointResolution(next, 1 / nextMetersPerUnit, newCenter, 'm') * nextMetersPerUnit;
return (resolution * currentPointResolution) / nextPointResolution;
}How the React version works
A map of Poland switched between WGS 84 and Web Mercator while a pixel keeps covering the same ground, worked out with getPointResolution, under a metric scale bar.
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. Both versions import resolutionInProjection, the resolution that keeps the scale in the new projection from the example's .shared.ts file, so the logic exists once.
From Angular to React
| Angular | React |
|---|---|
signals of the projection and the resolution | useState |
viewChild of the map | useRef<OLMap> on Map |
<aol-view [projection]="projection()" [resolution]="resolution()"> | <View projection={projection} resolution={resolution}> |
<select (change)="changeProjection($event)"> | <select value={projection} onChange={…}> |
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
Inside changeProjection, the current view centre is projected via transform(center, current, next). Calling getPointResolution at the centre in both the old and new projections measures how many ground metres one map unit covers in each CRS, scaling the old resolution by their ratio to set resolution alongside projection (starting at [zoom]="6" before the first switch).
Configuring the scale control with [bar]="true", [steps]="4" and [text]="true" makes the preserved 1 : N ratio directly verifiable.
How it works
changeProjectionreprojects the active centre viatransform(center, current, next)when switching CRS.getPointResolutionmeasures ground metres per map unit at the centre in both projections so their ratio preserves ground scale.- Initial startup uses
[zoom]="6", after which explicitresolutionandprojectionsignals build each replacement view. - Scale bar inputs
[bar]="true",[steps]="4"and[text]="true"display the representative fraction before and after switching.
Pitfalls
- In non-conformal projections such as
EPSG:4326, east–west and north–south pixel ground lengths differ by ~36% at 50° N—OpenLayers’ metric scale computation averages both directions, so neither axis matches conformalEPSG:3857identically. - Displayed 1 : N ratios assume the OGC standard 0.28 mm pixel pitch (~90.7 DPI) rather than querying physical monitor DPI.
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.