Created
September 10, 2019 08:56
-
-
Save clementgayvallet/544264c69fa0118db44e52e293a0590c to your computer and use it in GitHub Desktop.
Google Polyline Encoder
This file contains 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
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