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.

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.tsimport { 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 {} 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.
display-geojson-source.react.tsximport {
Coordinate,
DefaultControls,
DefaultInteractions,
GeoJSONSource,
Map,
OSMSource,
TileLayer,
VectorLayer,
View,
} from 'cc-openlayers/react';
/** A vector layer loaded from a GeoJSON file: Poland's voivodeships (GUGiK's PRG) around Kraków. */
export function DisplayGeojsonSource() {
return (
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
<View zoom={7}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
<VectorLayer>
<GeoJSONSource url="assets/poland/voivodeships.geojson" />
</VectorLayer>
</Map>
);
}How the React version works
Poland's voivodeships (GUGiK's PRG) loaded from a GeoJSON file into a vector layer over OpenStreetMap.
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 |
|---|---|
<aol-source-geojson> | <GeoJSONSource> |
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
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-geojsonfetches 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.
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.