Vector data · Angular, React and OpenLayers

Load GeoJSON from a URL

Fetch and render Polish voivodeship polygons from a bundled GeoJSON URL in OpenLayers with automatic CRS transformation into Web Mercator.

A map of southern Poland around Kraków with the outlines of the voivodeships from GeoJSON over OpenStreetMap, Małopolska in the middle.
A screenshot of the example. Voivodeships: Państwowy Rejestr Granic © GUGiK. Map data © OpenStreetMap contributors.

What this example shows

Centred on Małopolska and Kraków over southern Poland, the administrative outlines of Polish voivodeships stream from a packaged GeoJSON file and render in OpenLayers’ crisp default blue stroke and translucent fill atop OpenStreetMap.

When you need it

GeoJSON is the standard interchange format of modern web APIs, static administrative boundary bundles and spatial microservices.

Loading reference GeoJSON via URL keeps multi-megabyte coordinate arrays out of your compiled JavaScript bundle and lets browser HTTP caches reuse them across sessions.

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.

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

/** A vector layer loaded from a GeoJSON file: Poland's voivodeships (GUGiK's PRG) around Kraków. */
@Component({
  selector: 'app-display-geojson-source',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerTileComponent,
    LayerVectorComponent,
    MapComponent,
    SourceGeoJSONComponent,
    SourceOsmComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px">
      <aol-interaction-default />
      <aol-control-defaults />
      <aol-view [zoom]="7"><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-geojson url="assets/poland/voivodeships.geojson" />
      </aol-layer-vector>
    </aol-map>
  `,
})
export class DisplayGeojsonSource {}

Architecture and OpenLayers mechanics

Mounting aol-source-geojson inside a vector layer creates an OpenLayers VectorSource configured with the built-in GeoJSON format parser. When the file arrives in standard RFC 7946 WGS84 degrees, the format automatically projects every vertex into the map view’s Web Mercator coordinate system once and caches the resulting features in memory across all subsequent pans and zooms.

How it works

  • aol-source-geojson fetches the GeoJSON document and parses its features using OpenLayers’ GeoJSON format.
  • Incoming geographic WGS84 coordinates transform automatically into the active view projection during parsing.
  • With no custom style supplied, the vector layer paints OpenLayers’ built-in light fill and blue boundary stroke.
  • Features parse once into memory and render smoothly across subsequent pans and zooms without network re-fetches.

Pitfalls

  • A single GeoJSON URL downloads and parses the entire dataset at once—simplify dense national vertex geometries beforehand or switch to vector tiles when files grow into tens of megabytes.
  • This example ships its GeoJSON with the app; a cross-origin endpoint must still return permissive CORS headers for your application’s origin, otherwise fetch security blocks the request and leaves the layer empty.

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.