EncodedPolyline Class

Encodes and decodes the positions of a line with the encoded polyline algorithm of Google, used by Google Maps, OSRM, Valhalla or Strava, such as _p~iF~ps|U_ulLnnqC_mqNvxq`@.

public static class EncodedPolyline

Remarks

The format stores the latitude then the longitude of each position, rounded to a number of decimal digits: 5 for Google Maps and OSRM by default, 6 for Valhalla and the polyline6 geometries of OSRM. It has no altitude: the decoded positions have none, and the altitudes are ignored when encoding.

The coordinates are rounded half away from zero, as the reference implementation, and each position is stored as the difference with the previous rounded position, so the rounding errors do not add up along the line.

Methods

NameDescription
Decode(string, int)Decodes the positions of an encoded polyline.
Encode(IEnumerable<GeoPosition>, int)Encodes positions as an encoded polyline.

Decode(string, int)

EncodedPolyline.cs

Decodes the positions of an encoded polyline.

public static IReadOnlyList<GeoPosition> Decode(string encoded, int precision = 5)

Parameters

encoded string
The encoded polyline.
precision int
The number of decimal digits of the coordinates: 5 for Google Maps and OSRM, 6 for Valhalla.

Returns

IReadOnlyList<GeoPosition> The positions, without altitude. Create a GeoLineString from them to use them as a geometry.

Exceptions

ArgumentOutOfRangeException
precision is not between 0 and 10.
FormatException
The text is not a valid encoded polyline.

Encode(IEnumerable<GeoPosition>, int)

EncodedPolyline.cs

Encodes positions as an encoded polyline.

public static string Encode(IEnumerable<GeoPosition> positions, int precision = 5)

Parameters

positions IEnumerable<GeoPosition>
The positions. Their altitudes are ignored.
precision int
The number of decimal digits of the coordinates: 5 for Google Maps and OSRM, 6 for Valhalla.

Returns

string The encoded polyline.

Exceptions

ArgumentOutOfRangeException
precision is not between 0 and 10.
ArgumentException
A coordinate is not finite or is too large to be encoded.