Created
December 7, 2010 21:52
-
-
Save aaronlidman/732493 to your computer and use it in GitHub Desktop.
a googlemaps v2 encoded polyline to a list of coordinates (for use with the backward v3)
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
// Decode an encoded polyline into a list of lat/lng tuples. | |
function decodeLine (encoded) { | |
var len = encoded.length; | |
var index = 0; | |
var array = []; | |
var lat = 0; | |
var lng = 0; | |
while (index < len) { | |
var b; | |
var shift = 0; | |
var 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; | |
array.push([lat * 1e-5, lng * 1e-5]); | |
} | |
return array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment