Styling and labels · Angular, React and OpenLayers
Arrows at the end of every segment
Draw interactive polylines over Kraków in OpenLayers with directional arrowheads automatically placed and rotated at the end of every segment.

What this example shows
Over a street map of central Kraków at zoom 13, clicking across the city draws a yellow multi-segment polyline that automatically places a crisp yellow arrowhead at the end of every straight segment—oriented along that segment’s exact travel bearing even while you rotate the map.
When you need it
One-way street editors, delivery-route planners, water/sewer flow networks and evacuation paths must communicate travel direction unambiguously along every turn of a polyline.
Generating segment arrowheads dynamically inside a style function keeps directional markers automatically updated as users draw or drag vertices.
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.
line-arrows.tsimport { ChangeDetectionStrategy, Component } from '@angular/core';
import {
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerTileComponent,
LayerVectorComponent,
MapComponent,
SourceOsmComponent,
SourceVectorComponent,
ViewComponent,
} from 'cc-openlayers';
import { DrawInteractionComponent } from 'cc-openlayers/editing';
import type Feature from 'ol/Feature';
import type LineString from 'ol/geom/LineString';
import Point from 'ol/geom/Point';
import Icon from 'ol/style/Icon';
import Stroke from 'ol/style/Stroke';
import Style from 'ol/style/Style';
/** Lines drawn on the map, each segment ending in an arrow turned in its direction. */
@Component({
selector: 'app-line-arrows',
imports: [
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
DrawInteractionComponent,
LayerTileComponent,
LayerVectorComponent,
MapComponent,
SourceOsmComponent,
SourceVectorComponent,
ViewComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<aol-map height="480px">
<aol-interaction-default />
<aol-control-defaults />
<!-- The view stays within one world east and west of the real one; geometries must be inside that range. -->
<aol-view [zoom]="13" [extent]="extent">
<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 [style]="style">
<aol-source-vector #source />
</aol-layer-vector>
<aol-interaction-draw type="LineString" [source]="source.instance" />
</aol-map>
<div class="example-controls">
<p class="example-hint">Click to draw a line: every segment gets an arrow at its end.</p>
</div>
`,
})
export class LineArrows {
protected readonly extent = [-40075016.68557849, -20037508.342789244, 40075016.68557849, 20037508.342789244];
protected readonly style = (feature: Feature): Style[] => {
const geometry = feature.getGeometry() as LineString;
const styles = [new Style({ stroke: new Stroke({ color: '#ffcc33', width: 2 }) })];
geometry.forEachSegment((start, end) => {
const rotation = Math.atan2(end[1] - start[1], end[0] - start[0]);
styles.push(
new Style({
geometry: new Point(end),
image: new Icon({ src: 'assets/arrow.png', anchor: [0.75, 0.5], rotateWithView: true, rotation: -rotation }),
})
);
});
return styles;
};
} 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.
line-arrows.react.tsximport { useState } from 'react';
import type { FeatureLike } from 'ol/Feature.js';
import type LineString from 'ol/geom/LineString.js';
import Point from 'ol/geom/Point.js';
import type OLVectorSource from 'ol/source/Vector.js';
import Icon from 'ol/style/Icon.js';
import Stroke from 'ol/style/Stroke.js';
import Style from 'ol/style/Style.js';
import {
Coordinate,
DefaultControls,
DefaultInteractions,
DrawInteraction,
Map,
OSMSource,
TileLayer,
VectorLayer,
VectorSource,
View,
} from 'cc-openlayers/react';
const EXTENT = [-40075016.68557849, -20037508.342789244, 40075016.68557849, 20037508.342789244];
function style(feature: FeatureLike): Style[] {
const geometry = feature.getGeometry() as LineString;
const styles = [new Style({ stroke: new Stroke({ color: '#ffcc33', width: 2 }) })];
geometry.forEachSegment((start, end) => {
const rotation = Math.atan2(end[1] - start[1], end[0] - start[0]);
styles.push(
new Style({
geometry: new Point(end),
image: new Icon({ src: 'assets/arrow.png', anchor: [0.75, 0.5], rotateWithView: true, rotation: -rotation }),
})
);
});
return styles;
}
/** Lines drawn on the map, each segment ending in an arrow turned in its direction. */
export function LineArrows() {
const [source, setSource] = useState<OLVectorSource | null>(null);
return (
<>
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
{/* The view stays within one world east and west of the real one; geometries must be inside that range. */}
<View zoom={13} extent={EXTENT}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
<VectorLayer style={style}>
<VectorSource ref={setSource} />
</VectorLayer>
{source ? <DrawInteraction type="LineString" source={source} /> : null}
</Map>
<div className="example-controls">
<p className="example-hint">Click to draw a line: every segment gets an arrow at its end.</p>
</div>
</>
);
}How the React version works
Lines drawn on the map, each segment ending in an arrow turned in its direction; the view stays within one world east and west of the real one.
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-interaction-draw type="LineString" [source]> | <DrawInteraction type="LineString" source> |
viewChild of the source handed to the interactions | ref={setSource} into useState; the interactions get the source once the adapter has made it |
a style function as the layer's [style] | the same module function passed as style |
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
Alongside the base yellow line stroke, the vector layer’s style function calls forEachSegment on the feature’s LineString. For each [start, end] pair it computes dx and dy, derives the Cartesian angle via Math.atan2(dy, dx), and pushes an Icon style whose geometry is a Point at end and whose clockwise canvas rotation equals -angle.
Setting anchor: [0.75, 0.5] places the segment vertex three-quarters along the 12-pixel arrow graphic, and rotateWithView: true keeps arrowheads aligned with the street network when the camera rotates.
How it works
forEachSegmentiterates over each [start, end] pair of the drawnLineStringand appends a point-anchored arrow Icon style atend.- Negating
Math.atan2converts counter-clockwise Cartesian map angles into OpenLayers’ clockwise icon rotation. anchor: [0.75, 0.5]positions the vertex slightly inside the 12-pixel arrowhead so its tip extends cleanly without overshooting.rotateWithView: trueensures arrowheads turn with the line geometry whenever the map view rotates.
Pitfalls
- The view’s extent limits horizontal coordinates to one world in each direction, so drawn lines stay inside valid Web Mercator longitudes.
- Appending one Icon style per segment is ideal for hand-drawn routes of dozens of vertices, whereas dense GPS tracks with 10,000 vertices should simplify geometry or sample arrows every N metres instead.
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.