PolylineEncoding

Encodes and decodes coordinate sequences using the Encoded Polyline Algorithm.

The encoding logic is run following steps. Decoding the same in reversed order.

  1. Take the initial signed value

  2. Take the decimal value and multiply it by 10^precision, rounding the result

  3. Convert the decimal value to binary. Note that a negative value must be calculated using its two's complement by inverting the binary value and adding one to the result:

  4. Left-shift the binary value one bit:

  5. If the original decimal value is negative, invert this encoding

  6. Break the binary value out into 5-bit chunks (starting from the right hand side)

  7. Place the 5-bit chunks into reverse order

  8. OR each value with 0x20 if another bit chunk follows

  9. Convert each value to decimal

  10. Add 63 to each value

  11. Convert each value to its ASCII equivalent

See also

Functions

Link copied to clipboard
fun decode(encoded: String, precision: Int = 5): List<Position>

Decode an encoded polyline string to a list of positions.

Link copied to clipboard
fun decodeOrNull(encoded: String, precision: Int = 5): List<Position>?

Decode an encoded polyline string to a list of positions, returning null if the string is malformed or truncated.

Link copied to clipboard
fun encode(positions: List<Position>, precision: Int = 5): String

Encode a list of positions to an encoded polyline string.