Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Google Polyline Encoder
function encode(locations) {
var lastLat = 0.0;
var lastLng = 0.0;
var result = "";
for (i in locations) {
var location = locations[i];
var lat = Math.round(location[1] * 1e5);
var lng = Math.round(location[0] * 1e5);
var dLat = lat - lastLat;
var dLng = lng - lastLng;
result += encodeCoordinate(dLat);
result += encodeCoordinate(dLng);
lastLat = lat;
lastLng = lng;
}
return result;
}
function encodeCoordinate(coord) {
var result = "";
coord = coord << 1;
if (coord < 0) {
coord = ~coord;
}
while (coord >= 0x20) {
result += String.fromCodePoint((0x20 | (coord & 0x1F)) + 63);
coord = coord >> 5;
}
result += String.fromCodePoint(coord + 63);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.