/google-polyline

Encode / decode Google's polyline format

Primary LanguageJavaScriptMIT LicenseMIT

NPM version Build Status Dependency Status

google-polyline

Encodes and decodes Google's polyline format It's a forked version of google-polyline module with minor performance improvements and reversed order of coordinates: longitude, latitude (think x, y or easting, northing), which corresponds to position definition in GeoJSON spec.

Install via npm

$ npm install --save google-polyline

Usage

var polyline = require( 'google-polyline' )
polyline.encode([
  [ -120.2, 38.5 ],
  [ -120.95, 40.7 ],
  [ -126.453, 43.252 ]
])

> '_p~iF~ps|U_ulLnnqC_mqNvxq`@'
polyline.decode( '_p~iF~ps|U_ulLnnqC_mqNvxq`@' )

> [
  [ -120.2, 38.5 ],
  [ -120.95, 40.7 ],
  [ -126.453, 43.252 ]
]

API

polyline.encode(points)

returns string representing encoded polyline

  • points is an array of points, each point is a 2 element array [longitude, latitude]

polyline.decode(string, factor)

returns array of point representing decoded polyline

  • string is an encoded polyline representation
  • factor - optional (defaults to 10e5), factor by which coordinates are divided after decoding; use factor 10e6 when decoding polylines from OSM data (OSRM, mapzen etc.)