• Stars
    star
    176
  • Rank 216,987 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 12 years ago
  • Updated 12 months ago

Reviews

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

Repository Details

Plugin to support Google's polyline encoding in Leaflet.

Support encoded polylines in Leaflet

This Leaflet plugin extends Leaflet with functions to encode to and decode from Google maps polyline encoding.

Escaping backslashes

Make sure to always escape the backslashes in encoded strings! Not doing so will result in the backslash to be interpreted as an escape character, yielding wrong results.

API

Utility methods

  • L.PolylineUtil.encode(latlngs [, precision])

    Encode an array of L.LatLng objects, or an array of arrays.

  • L.PolylineUtil.decode(encoded [, precision])

    Decode the string encoded to an array of [lat, lng]-arrays.

Extensions for L.Polyline

  • L.Polyline.fromEncoded(encoded [, options])

    Construct a new L.Polyline from a string, with optional options object. Backslashes in strings should be properly escaped.

  • L.Polyline.encodePath()

    Return an encoded string for the current Polyline.

Extensions for L.Polygon

  • L.Polygon.fromEncoded(encoded [, options])

    Construct a new L.Polygon from a string, with optional options object. Backslashes in strings should be properly escaped.

  • L.Polygon.encodePath()

    Return an encoded string for the current Polygon.

Code examples

After loading leaflet.js, src/Polyline.encoded.js should be included.

Encoding

var latlngs = [
	[38.5, -120.5],
	[40.7, -120.95],
	[43.252, -126.453]
];
var polyline = L.polyline(latlngs);

// prints "_p~iF~cn~U_ulLn{vA_mqNvxq`@" to the console
console.log(polyline.encodePath());

Decoding

var encoded = "_p~iF~cn~U_ulLn{vA_mqNvxq`@";
var polyline = L.Polyline.fromEncoded(encoded);

// prints an array of 3 LatLng objects.
console.log(polyline.getLatLngs());

Use a decoding precision of 5 to decode OSRM Routing Engine geometries

var encoded = "_izlhA~pvydF_{geC~{mZ_kwzCn`{nI";
var polyline = L.polyline(L.PolylineUtil.decode(encoded, 5));

// prints an array of 3 LatLng objects.
console.log(polyline.getLatLngs());

Node package

You can use encode() and decode() in your Nodejs scripts:

npm install polyline-encoded

var polyUtil = require('polyline-encoded');

var encoded = "_p~iF~cn~U_ulLn{vA_mqNvxq`@";
var latlngs = polyUtil.decode(encoded);

More Repositories

1

django-tables2

django-tables2 - An app for creating HTML tables
Python
1,850
star
2

Leaflet.Sync

Synchronized view of two maps.
JavaScript
235
star
3

Leaflet-semicircle

Extend Leaflet's circle class to display semicircles.
JavaScript
120
star
4

leaflet-headless

Leaflet for node.
JavaScript
117
star
5

orc-data

Display/visualize some freely available ORC certificate sailboat data.
Svelte
72
star
6

python-lora

Decrypt LoRa payloads in python.
Python
67
star
7

leaflet.layerscontrol-minimap

Leaflet layers control with minimaps.
JavaScript
49
star
8

marinetraffic

Node module to use marinetraffic tracks.
JavaScript
26
star
9

leaflet-clonelayer

Clone leaflet layers
JavaScript
25
star
10

Leaflet.Grid

Displays a lat/lng grid overlay on Leaflet maps.
JavaScript
22
star
11

Leaflet.latlngcontrol

Show and edit the lat/lng for the center of a leaflet map
JavaScript
5
star
12

grunt-geojsonhint

Grunt task to run geojsonhint
JavaScript
3
star
13

chai-leaflet

Chai assertions to use with Leaflet map apps
JavaScript
3
star
14

fph-parser

Read Fisher & Paykel CPAP .FPH files.
Python
3
star
15

laser-gcode-swatch

Generate a series of paths at different feedrates / laser power, to check the effect of your CNC laser etcher on different materials.
JavaScript
3
star
16

f-engrave

Fork of f-engrave
Python
3
star
17

Compare-NewYork

Compare 1836 and modern New York
JavaScript
2
star
18

d3-legend

Simple legend for d3 using data-legend attributes on paths.
JavaScript
2
star
19

coconut-graph

Graph lib using d3
JavaScript
2
star
20

leaflet-layerscontrol-buttons

Leaflet layers control using a set of buttons.
CSS
2
star
21

saillogs

Stories about sailing in dutch.
JavaScript
2
star
22

rws2gpx

Rijkswaterstaat drijvende markeringen naar GPX converteren
HTML
2
star
23

svelte-django-gettext

Use Django's translation catalog in Svelte
JavaScript
2
star
24

NL-travel-advice

Travel advice from The Netherlands ministry of foreign affairs; JSON + map
JavaScript
1
star
25

NL-social-churches

Lijst van kerken in NL die actief zijn op social media met wat cijfertjes.
JavaScript
1
star