Created
September 28, 2012 03:22
-
-
Save vinh0604/3797775 to your computer and use it in GitHub Desktop.
Google directions polylines Javascript decoder
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
| function DecodePolylines(encoded) { | |
| var poly = []; | |
| var index = 0, len = encoded.length, | |
| lat = 0, lng = 0; | |
| while (index < len) { | |
| var b, shift = 0, result = 0; | |
| do { | |
| b = encoded.charCodeAt(index++) - 63; | |
| result |= (b & 0x1f) << shift; | |
| shift += 5; | |
| } while (b >= 0x20); | |
| var dlat = ((result & 1) ? ~(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); | |
| var dlng = ((result & 1) ? ~(result >> 1) : (result >> 1)); | |
| lng += dlng; | |
| var p = { | |
| latitude: lat * 1E-5, | |
| longitude: lng * 1E-5 | |
| }; | |
| poly.push(p); | |
| } | |
| return poly; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment