Vector data · Angular, React and OpenLayers
A rectangle from a bounding box
Draw a closed geographic bounding-box polygon over Kraków in an OpenLayers template using aol-geometry-polygon and EPSG:4326 corner coordinates.

What this example shows
At zoom 11 over OpenStreetMap, a crisp rectangular bounding box spanning western and central Kraków (19.8°–20.07° E, 49.99°–50.13° N) renders with OpenLayers’ default translucent white fill and blue outline directly from four corner values in component state.
When you need it
Map sheet indexes, spatial export tools, imagery footprints and search-area previews constantly visualise [minLon, minLat, maxLon, maxLat] bounding boxes on the map.
Constructing a five-point closed polygon ring in the template keeps simple bounding-box overlays declarative and synchronized with form bounds.
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.
rectangle.tsimport { ChangeDetectionStrategy, Component } from '@angular/core';
import {
CollectionCoordinatesComponent,
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
FeatureComponent,
GeometryPolygonComponent,
LayerTileComponent,
LayerVectorComponent,
MapComponent,
SourceOsmComponent,
SourceVectorComponent,
ViewComponent,
} from 'cc-openlayers';
/** A rectangular polygon, the corners of a bounding box, over an OpenStreetMap base map. */
@Component({
selector: 'app-rectangle',
imports: [
CollectionCoordinatesComponent,
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
FeatureComponent,
GeometryPolygonComponent,
LayerTileComponent,
LayerVectorComponent,
MapComponent,
SourceOsmComponent,
SourceVectorComponent,
ViewComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<aol-map height="480px">
<aol-interaction-default />
<aol-control-defaults />
<aol-view [zoom]="11"><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-polygon>
<aol-collection-coordinates [coordinates]="box" srid="EPSG:4326" />
</aol-geometry-polygon>
</aol-feature>
</aol-source-vector>
</aol-layer-vector>
</aol-map>
`,
})
export class Rectangle {
/** The box from the minimum to the maximum longitude and latitude, as the ring of a polygon. */
protected readonly box = [
[
[19.8, 49.99],
[20.07, 49.99],
[20.07, 50.13],
[19.8, 50.13],
[19.8, 49.99],
],
];
} 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.
rectangle.react.tsximport {
CollectionCoordinates,
Coordinate,
DefaultControls,
DefaultInteractions,
Feature,
Map,
OSMSource,
PolygonGeometry,
TileLayer,
VectorLayer,
VectorSource,
View,
} from 'cc-openlayers/react';
/** The box from the minimum to the maximum longitude and latitude, as the ring of a polygon. */
const BOX = [
[
[19.8, 49.99],
[20.07, 49.99],
[20.07, 50.13],
[19.8, 50.13],
[19.8, 49.99],
],
];
/** A rectangular polygon, the corners of a bounding box, over an OpenStreetMap base map. */
export function Rectangle() {
return (
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
<View zoom={11}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
<VectorLayer>
<VectorSource>
<Feature>
<PolygonGeometry>
<CollectionCoordinates coordinates={BOX} srid="EPSG:4326" />
</PolygonGeometry>
</Feature>
</VectorSource>
</VectorLayer>
</Map>
);
}How the React version works
A rectangular polygon from the corners of an extent around Kraków 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-geometry-polygon> with <aol-collection-coordinates srid="EPSG:4326"> | <PolygonGeometry> with <CollectionCoordinates srid="EPSG:4326"> |
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
Component property box arranges the minimum and maximum longitude/latitude into a five-vertex linear ring—repeating the starting corner at index 4 to close the polygon—and passes that ring into aol-geometry-polygon via aol-collection-coordinates with srid="EPSG:4326" so OpenLayers transforms the corners into Web Mercator.
How it works
boxbuilds the closed five-coordinate ring from minimum and maximum longitude/latitude values, repeating the first corner last.aol-geometry-polygonandaol-collection-coordinateswithsrid="EPSG:4326"project degree corners into the view CRS.- The rectangle covers 19.8°–20.07° E and 49.99°–50.13° N across Kraków using OpenLayers’ default vector style.
Pitfalls
- Transforming only four corners keeps edges straight along meridians and parallels in cylindrical Web Mercator, whereas conic or polar projections curve parallels—densify long east-west edges with intermediate vertices before reprojecting onto conic grids.
- GeoJSON and OGC WKT specifications strictly require linear rings to repeat their initial vertex at the end; a four-point unclosed array will be rejected by spatial backends.
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.