LineString Arrows

draw12 vector65 arrow1

Example of drawing arrows for each line string segment.

Example of drawing arrows for each line string segment.

import 'ol/ol.css';
import Draw from 'ol/interaction/Draw';
import Map from 'ol/Map';
import Point from 'ol/geom/Point';
import View from 'ol/View';
import {Icon, Stroke, Style} from 'ol/style';
import {OSM, Vector as VectorSource} from 'ol/source';
import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer';

const raster = new TileLayer({
  source: new OSM(),

const source = new VectorSource();

const styleFunction = function (feature) {
  const geometry = feature.getGeometry();
  const styles = [
    // linestring
    new Style({
      stroke: new Stroke({
        color: '#ffcc33',
        width: 2,

  geometry.forEachSegment(function (start, end) {
    const dx = end[0] - start[0];
    const dy = end[1] - start[1];
    const rotation = Math.atan2(dy, dx);
    // arrows
      new Style({
        geometry: new Point(end),
        image: new Icon({
          src: 'data/arrow.png',
          anchor: [0.75, 0.5],
          rotateWithView: true,
          rotation: -rotation,

  return styles;
const vector = new VectorLayer({
  source: source,
  style: styleFunction,

const map = new Map({
  layers: [raster, vector],
  target: 'map',
  view: new View({
    center: [-11000000, 4600000],
    zoom: 4,

  new Draw({
    source: source,
    type: 'LineString',
<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <title>LineString Arrows</title>
    <!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
    <script src="https://unpkg.com/elm-pep"></script>
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v3/polyfill.min.js?features=fetch,requestAnimationFrame,Element.prototype.classList,URL,TextDecoder,Number.isInteger"></script>
      .map {
        width: 100%;
    <div id="map" class="map"></div>
    <script src="main.js"></script>
  "name": "line-arrows",
  "dependencies": {
    "ol": "6.8.1"
  "devDependencies": {
    "parcel": "^2.0.0-beta.1"
  "scripts": {
    "start": "parcel index.html",
    "build": "parcel build --public-url . index.html"