Skip to content

Instantly share code, notes, and snippets.

@vinh0604
Created September 28, 2012 03:22
Show Gist options
  • Select an option

  • Save vinh0604/3797775 to your computer and use it in GitHub Desktop.

Select an option

Save vinh0604/3797775 to your computer and use it in GitHub Desktop.
Google directions polylines Javascript decoder
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