Created
January 14, 2020 16:26
-
-
Save valkiie/a20dbc088dffa62374aca5fe3cf36291 to your computer and use it in GitHub Desktop.
Just a refactor of graphhopper's decodePath (https://github.com/graphhopper/directions-api-js-client/blob/master/src/GHUtil.js#L27)
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
public decode(encoded, index){ | |
let b, result=0, shift=0 | |
do { | |
b = encoded.charCodeAt(index++) - 63; | |
result |= (b & 0x1f) << shift; | |
shift += 5; | |
} while (b >= 0x20); | |
return [result,index] | |
} | |
public decodePath(encoded, is3D){ | |
let len = encoded.length; | |
let index = 0; | |
let array = []; | |
let lat = 0; | |
let lng = 0; | |
let ele = 0; | |
while (index < len) { | |
let result = 0; | |
[result,index] = this.decode(encoded,index) | |
let deltaLat = ((result & 1) ? ~(result >> 1) : (result >> 1)); | |
lat += deltaLat; | |
[result,index] = this.decode(encoded,index) | |
let deltaLon = ((result & 1) ? ~(result >> 1) : (result >> 1)); | |
lng += deltaLon; | |
if (is3D) { | |
// elevation | |
[result,index] = this.decode(encoded,index) | |
let deltaEle = ((result & 1) ? ~(result >> 1) : (result >> 1)); | |
ele += deltaEle; | |
array.push([lng * 1e-5, lat * 1e-5, ele / 100]); | |
} else | |
array.push([lng * 1e-5, lat * 1e-5]); | |
} | |
return array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment