Skip to content

Instantly share code, notes, and snippets.

@niqueco
Last active December 31, 2015 20:27
Show Gist options
  • Save niqueco/a3923057f12f18d34f04 to your computer and use it in GitHub Desktop.
Save niqueco/a3923057f12f18d34f04 to your computer and use it in GitHub Desktop.
Google Maps polyline decoder in Wolfram Mathematica
(* Decoder for polylines as defined in https://developers.google.com/maps/documentation/utilities/polylinealgorithm
*)
decodePolyline[encoded_String] := Module[{deltas},
deltas = With[{x = FromDigits[#, 32]},
If[OddQ[x], -BitShiftRight[x, 1] - 1, BitShiftRight[x, 1]]
] & /@ Reverse[BitAnd[ToCharacterCode@StringCases[encoded, RegularExpression["[_-~]*[ -\\^]"]] - 63, BitNot@32], 2];
Rest@FoldList[Plus, {0, 0}, Partition[deltas / 100000., 2]]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment