Last active
January 10, 2024 07:04
-
-
Save Younes-Charfaoui/8cc6920b41b357bcdaf5ca98729eb11c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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