Skip to content

Instantly share code, notes, and snippets.

@ysfzrn
Forked from ismaels/polyline_decoder.js
Created March 25, 2021 12:32
Show Gist options
  • Save ysfzrn/553f504b567d4fc723d48c04c62aac33 to your computer and use it in GitHub Desktop.
Save ysfzrn/553f504b567d4fc723d48c04c62aac33 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