Built with blockbuilder.org
Last active
January 12, 2017 21:54
-
-
Save burritojustice/9a8917ae0f0713d87e6a71371fc73ea1 to your computer and use it in GitHub Desktop.
polyline decoder
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
license: mit |
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
<!DOCTYPE html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<style> | |
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } | |
</style> | |
</head> | |
<body> | |
<script> | |
// This is adapted from the implementation in Project-OSRM | |
// https://github.com/DennisOSRM/Project-OSRM-Web/blob/master/WebContent/routing/OSRM.RoutingGeometry.js | |
function polyline_decode(str, precision) { | |
var index = 0, | |
lat = 0, | |
lng = 0, | |
coordinates = [], | |
shift = 0, | |
result = 0, | |
byte = null, | |
latitude_change, | |
longitude_change, | |
factor = Math.pow(10, precision || 6); | |
// Coordinates have variable length when encoded, so just keep | |
// track of whether we've hit the end of the string. In each | |
// loop iteration, a single coordinate is decoded. | |
while (index < str.length) { | |
// Reset shift, result, and byte | |
byte = null; | |
shift = 0; | |
result = 0; | |
do { | |
byte = str.charCodeAt(index++) - 63; | |
result |= (byte & 0x1f) << shift; | |
shift += 5; | |
} while (byte >= 0x20); | |
latitude_change = ((result & 1) ? ~(result >> 1) : (result >> 1)); | |
shift = result = 0; | |
do { | |
byte = str.charCodeAt(index++) - 63; | |
result |= (byte & 0x1f) << shift; | |
shift += 5; | |
} while (byte >= 0x20); | |
longitude_change = ((result & 1) ? ~(result >> 1) : (result >> 1)); | |
lat += latitude_change; | |
lng += longitude_change; | |
coordinates.push([lat / factor, lng / factor]); | |
} | |
var points = JSON.stringify(coordinates); | |
console.log(points); | |
return coordinates; | |
}; | |
var input = 'g~iagArdumhFbF`HrVq\\tYm_@jBhClO~RvX~^`\\tc@dEbGlAzAhVp\\jBfCj`@~h@jAxAnDdF`GnIfJhM~HzK`CdDre@so@tJiMtIiLvNqRtZya@bKgN~IkLl@{@vW_^jG]dELvDl@zE|@tExA|ExBzEfDzFfErFbFpGpG`H`HzG~HhWb\\|DbFvHlJze@nh@|JvN|JtOdJtOnIvNfItOdcAzqBrFlKbGlJpGlJhH~HvH~HxHrGfIpGdJrFlJtElJvDdKdDzJzBlKhBzJz@tK^bKLdKMjK_@jKk@dKmAvIkAtI{AhSsF`MuD|IwDtYwMhHwChHyCrFiB~HiCxHyBlIiBtKyBlJ{AtJkAfIm@zK}@baDuNbe@mA~qAaGtJO~HOrKNbK\\dK|@tJz@|JjBzJxBfJfClJfD|IvD~ItEtIdE`]`RhHfDfIvDnHfC`IxBfIzA~HjA~H|@fI\\nI?nI]nIm@dJkA|IkBnJwCfIwCtIuE~HuEhIqGfIqH`HoH~GoJrG}IhGmJvN}TdF_I`GoIjFqGzFsGzFcFbGeFxGsE`HwDvIwDfIwCfIyB|I{AlJkAdK{@bKm@tJ]rbC{K~N_@tO]tNOxb@pHxGjApGzAzKvCzV~HflA~^~Bj@`ClAxBxAtEfDfDvDhCtExBbF`BdFrApGjAbGtEvX~I`{@bA~HrApHzApGjFfX|Jhb@jAbGbBfN\\`GLnIFxW\\ry@^vXF~]TxWNj`@?jAT|_@Vj`@d@`f@D|T?rFFtEt@h`A?fDFxC\\hu@bAlgB^pg@zEfCfqAlt@ld@hWnc@jVrAz@||Ap{@xBz_AFxBkB}@' | |
polyline_decode(input); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment