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.

OpenStreetMap of Kraków at zoom 13 with a yellow zigzag line drawn across the city and a yellow arrowhead at the end of each of its four segments.
A screenshot of the example. Map data © OpenStreetMap contributors.

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

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

  • forEachSegment iterates over each [start, end] pair of the drawn LineString and appends a point-anchored arrow Icon style at end.
  • Negating Math.atan2 converts 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: true ensures 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.

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.