polyline encoding and decoding in javascript
Branch: master
Clone or download
Latest commit eaa1f05 Apr 1, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin Add --toGeoJSON option Apr 1, 2018
src Use a conditional rather than Math.sign Nov 7, 2017
test Fix issue with negative rounding Jun 10, 2016
.eslintrc Add eslintrc Jun 21, 2015
.gitignore Add yarn.lock Apr 1, 2018
.travis.yml use newer node Dec 22, 2016
API.md Add toGeoJSON() function Jul 19, 2015
CHANGELOG.md Update CHANGELOG Apr 1, 2018
LICENSE Initial commit Jul 19, 2012
Makefile Travis test, make, readme Jul 26, 2012
README.md Update README.md Apr 1, 2018
index.js Initial commit Jul 19, 2012
package.json v1.0.0 Apr 1, 2018
yarn.lock Update devDependencies Apr 1, 2018


Build Status Coverage Status


A simple google-esque polyline implementation in Javascript. Compatible with nodejs (npm install @mapbox/polyline and the browser (copy src/polyline.js)).

Encodes/decodes into [lat, lng] coordinate pairs. Use fromGeoJSON() to encode from GeoJSON objects, or toGeoJSON to decode to a GeoJSON LineString.


npm install @mapbox/polyline

Note that the old package polyline has been deprecated in favor of @mapbox/polyline (the old package remain but won't receive updates).


var polyline = require('@mapbox/polyline');

// returns an array of lat, lon pairs

// returns a string-encoded polyline
polyline.encode([[38.5, -120.2], [40.7, -120.95], [43.252, -126.453]]);

// returns a string-encoded polyline from a GeoJSON LineString
polyline.fromGeoJSON({ "type": "Feature",
  "geometry": {
    "type": "LineString",
    "coordinates": [[-120.2, 38.5], [-120.95, 40.7], [-126.453, 43.252]]
  "properties": {}

API Documentation

Command line

Install globally or run ./node_modules/.bin/polyline.

Send input via stdin and use --decode, --encode, --toGeoJSON, or --fromGeoJSON flags. If omitted will default to --decode.

Example :

cat file.json | ./bin/polyline.bin.js --fromGeoJSON > result.txt