Skip to content

Instantly share code, notes, and snippets.

@Younes-Charfaoui
Last active January 10, 2024 07:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Younes-Charfaoui/8cc6920b41b357bcdaf5ca98729eb11c to your computer and use it in GitHub Desktop.
Save Younes-Charfaoui/8cc6920b41b357bcdaf5ca98729eb11c to your computer and use it in GitHub Desktop.
fun decode(encodedPath: String): List<LatLng> {
val len = encodedPath.length
// For speed we preallocate to an upper bound on the final length, then
// truncate the array before returning.
val path: MutableList<LatLng> = ArrayList()
var index = 0
var lat = 0
var lng = 0
while (index < len) {
var result = 1
var shift = 0
var b: Int
do {
b = encodedPath[index++].code - 63 - 1
result += b shl shift
shift += 5
} while (b >= 0x1f)
lat += if (result and 1 != 0) (result shr 1).inv() else result shr 1
result = 1
shift = 0
do {
b = encodedPath[index++].code - 63 - 1
result += b shl shift
shift += 5
} while (b >= 0x1f)
lng += if (result and 1 != 0) (result shr 1).inv() else result shr 1
path.add(LatLng(lat * 1e-5, lng * 1e-5))
}
return path
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment