Polyline
The polyline-encoding module encodes and decodes polylines using
Google’s Encoded Polyline Algorithm Format.
val positions = listOf( Position(longitude = -120.2, latitude = 38.5), Position(longitude = -120.95, latitude = 40.7), Position(longitude = -126.453, latitude = 43.252), )val encoded = PolylineEncoding.encode(positions)val decoded = PolylineEncoding.decode(encoded)Details can be found in the API reference.
Installation
Section titled “Installation”commonMain { dependencies { implementation("org.maplibre.spatialk:polyline-encoding:0.8.0") }}dependencies { implementation("org.maplibre.spatialk:polyline-encoding-jvm:0.8.0")}Encoding
Section titled “Encoding”PolylineEncoding.encode converts a list of Position values into a compact encoded string.
val encoded = PolylineEncoding.encode( listOf( Position(longitude = -120.2, latitude = 38.5), Position(longitude = -120.95, latitude = 40.7), ) )Decoding
Section titled “Decoding”PolylineEncoding.decode converts an encoded polyline string back into a list of Position values.
It throws IllegalArgumentException if the input is malformed.
val positions = PolylineEncoding.decode("_p~iF~ps|U_ulLnnqC")PolylineEncoding.decodeOrNull behaves the same way, but returns null on malformed input instead
of throwing.
val positions = PolylineEncoding.decodeOrNull("_p~iF~ps|U_ulLnnqC")Precision
Section titled “Precision”All three methods accept an optional precision parameter that controls the number of decimal
digits used for coordinate values. The default is 5, which matches the standard Google precision
of 1e-5. Some services, such as OSRM, use a precision of 6 for 1e-6.
val positions = listOf( Position(longitude = -120.2, latitude = 38.5), Position(longitude = -120.95, latitude = 40.7), )val encoded = PolylineEncoding.encode(positions, precision = 6)val decoded = PolylineEncoding.decode(encoded, precision = 6)