• Stars
    star
    493
  • Rank 89,306 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 12 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A plug-in for the JS map library Leaflet, allowing to define patterns (like dashes, arrows, icons, etc.) on Polylines.

Leaflet PolylineDecorator

CDNJS

A Leaflet plug-in to define and draw patterns on existing Polylines or along coordinate paths. Demo.

Compatibility with Leaflet versions

The current version of the plugin (on the master branch) works only with versions 1.* of Leaflet.

For a version of the plugin compatible with the older 0.7.* Leaflet releases, use the leaflet-0.7.2 branch. But this branch is not maintained anymore and Leaflet 1.* has been around for a while, so you should definitely update.

npm / bower

npm install leaflet-polylinedecorator
bower install leaflet-polylinedecorator

Features

  • Dashed or dotted lines, arrow heads, markers following line
  • Works on Polygons too! (easy, as Polygon extends Polyline)
  • Multiple patterns can be applied to the same line
  • New behaviors can be obtained by defining new symbols

Screenshot

screenshot

Usage

To create a decorator and add it to the map: L.polylineDecorator(latlngs, options).addTo(map);

  • latlngs can be one of the following types:

  • L.Polyline

  • L.Polygon

  • an array of L.LatLng, or with Leaflet's simplified syntax, an array of 2-cells arrays of coordinates (useful if you just want to draw patterns following coordinates, but not the line itself)

  • an array of any of these previous types, to apply the same patterns to multiple lines

  • options has a single property patterns, which is an array of Pattern objects.

Pattern definition

Property Type Required Description
offset see below No Offset of the first pattern symbol, from the start point of the line. Default: 0.
endOffset see below No Minimum offset of the last pattern symbol, from the end point of the line. Default: 0.
repeat see below Yes Repetition interval of the pattern symbols. Defines the distance between each consecutive symbol's anchor point.
symbol Symbol factory Yes Instance of a symbol factory class.

offset, endOffset and repeat can be each defined as a number, in pixels, or in percentage of the line's length, as a string (ex: '10%').

Methods

Method Description
setPaths(latlngs) Changes the path(s) the decorator applies to. latlngs can be all the types supported by the constructor. Useful for example if you remove polyline from a set, or coordinates change.
setPatterns(<Pattern[]> patterns) Changes the decorator's pattern definitions, and update the symbols accordingly.

Example

var polyline = L.polyline([...]).addTo(map);
var decorator = L.polylineDecorator(polyline, {
    patterns: [
        // defines a pattern of 10px-wide dashes, repeated every 20px on the line
        {offset: 0, repeat: 20, symbol: L.Symbol.dash({pixelSize: 10})}
    ]
}).addTo(map);

Performance note/alternatives

This plugin creates actual L.Layer objects (markers, polyline, etc.) to draw the pattern symbols. This is extra customizable as you can define your own symbols, but it may have an impact on the responsiveness of your map if you have to draw a lot of symbols on many large polylines.

Here are two light-weight alternatives for simpler cases:

More Repositories

1

Leaflet.RotatedMarker

Leaflet plugin to enable the rotation of map marker icons
JavaScript
257
star
2

Leaflet.PolylineOffset

Apply a relative pixel offset to polylines without changing their coordinates.
JavaScript
151
star
3

Leaflet.MagnifyingGlass

A plugin to add a customizable magnifying glass tool to a Leaflet map.
JavaScript
105
star
4

jkstra

JavaScript graph routing library
JavaScript
49
star
5

thread-reader-reader

Parse Twitter thread already simplified by third-party Thread Reader app, and produce dead simple HTML
JavaScript
20
star
6

FirefoxOS.GeoTracker

GPS track recording web app for Firefox OS… and mobile browsers in general.
JavaScript
20
star
7

bird-tracking

Interactive data viz of migrating birds
JavaScript
13
star
8

react-staticMapZoom

A React component which mimics the auto zoom mini-map found on geo-tagged photos Flickr pages.
JavaScript
10
star
9

clean-cnrtl

Clean and responsive proxy to CNRTL French dictionary website
CSS
10
star
10

simple-static-map-server

Raster map service based on MapLibre GL JS
JavaScript
9
star
11

metro-pili

JavaScript
5
star
12

irish-tune-map

A map of irish folk tunes
JavaScript
5
star
13

GeoTracker

JavaScript
3
star
14

updatable-priority-queue

A priority queue implementation based on a binary heap, with an efficient update-key operation
JavaScript
3
star
15

mapbox-gl-style-optimizer

Suggests optimizations to Mapbox-GL style files to improve rendering perfs.
JavaScript
3
star
16

place-place

Place-Place, a place positioning game to test spatial knowledge of your city
TypeScript
3
star
17

pw2014

Atelier Paris Web 2014
JavaScript
2
star
18

alt-route-labeller

A library to compute the best position to put labels on multiple routes between two points
JavaScript
2
star
19

bbecquet

1
star