Skip to content

Instantly share code, notes, and snippets.

@rudyhuynh
Forked from ismaels/polyline_decoder.js
Created April 3, 2017 06:26
Show Gist options
  • Save rudyhuynh/ff25e9e26fa00a2e943c632824a77c02 to your computer and use it in GitHub Desktop.
Save rudyhuynh/ff25e9e26fa00a2e943c632824a77c02 to your computer and use it in GitHub Desktop.
Javascript function to decode google maps api polyline
// source: http://doublespringlabs.blogspot.com.br/2012/11/decoding-polylines-from-google-maps.html
function decode(encoded){
// array that holds the points
var points=[ ]
var index = 0, len = encoded.length;
var lat = 0, lng = 0;
while (index < len) {
var b, shift = 0, result = 0;
do {
b = encoded.charAt(index++).charCodeAt(0) - 63;//finds ascii //and substract it by 63
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
var dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.charAt(index++).charCodeAt(0) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
var dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
points.push({latitude:( lat / 1E5),longitude:( lng / 1E5)})
}
return points
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment