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.

A map of Kraków in geographic coordinates with a marker in the Old Town and one in Nowa Huta, and the projection select set to EPSG:4326 and a Zoom in button below it.
A screenshot of the example. Map data © OpenStreetMap contributors.

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.ts
import { 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);
  }
}

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] on aol-view to 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:4326 triggers client-side triangle warping of Web Mercator OpenStreetMap tiles, which slightly softens raster text compared with native EPSG:3857 rendering.
  • Identical integer zoom numbers represent different physical ground scales in EPSG:3857 versus EPSG:4326—use getPointResolution (projection-and-scale) when ground scale must stay constant across switches.

Background: state and lifecycle of maps in Angular with OpenLayers, which the components of cc-openlayers take care of.