Encoding and Decoding Polylines with Erlang
If you have ever worked with the Google Directions API you probably came across encoded polylines. I wanted to decode and encode these using Erlang but was unable to find an existing implementation. So I decided to write my own.
- A single point (latitude/longitude pair) is represented using a tuple
- A path, i.e. a decoded polyline, is represented as a list of point tuples
- An encoded polyline is represented as a simple binary string
Point = {Lng, Lat},
Path = [{Lng, Lat}, {Lng2, Lat2}].
The encode/1
function takes a path and returns an encoded polyline.
|
|
The decode/1
function takes an encoded polyline and returns a path.
|
|
I have written these functions for noesis, which is a library that contains useful utility functions. Right now the implementation is only available in the development branch. It is tested using EUnit and QuickCheck.
If you’re reading this a couple of months from now, you might find an updated implementation on GitHub.