Created
December 20, 2023 10:00
-
-
Save micaiah-effiong/1b3da0071ccb0ca7e801d1209ca4e52f to your computer and use it in GitHub Desktop.
decode google maps polyline. compare your result to https://developers.google.com/maps/documentation/utilities/polylineutility
This file contains hidden or 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
| type LatLng = [number, number]; | |
| function decodePolyline(encoded: string): Array<LatLng> { | |
| const poly: Array<LatLng> = []; | |
| let index = 0; | |
| const len = encoded.length; | |
| let lat = 0, | |
| lng = 0; | |
| while (index < len) { | |
| let b, | |
| shift = 0, | |
| result = 0; | |
| do { | |
| b = encoded.charCodeAt(index++) - 63; | |
| result |= (b & 0x1f) << shift; | |
| shift += 5; | |
| } while (b >= 0x20); | |
| const dlat = (result & 1) != 0 ? ~(result >> 1) : result >> 1; | |
| lat += dlat; | |
| shift = 0; | |
| result = 0; | |
| do { | |
| b = encoded.charCodeAt(index++) - 63; | |
| result |= (b & 0x1f) << shift; | |
| shift += 5; | |
| } while (b >= 0x20); | |
| let dlng = (result & 1) != 0 ? ~(result >> 1) : result >> 1; | |
| lng += dlng; | |
| let p: LatLng = [lat / 1e5, lng / 1e5]; | |
| poly.push(p); | |
| } | |
| return poly; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment