Google Polyline Encoder / Decoder

Encode lat/lon pairs to Google Encoded Polyline strings, and back

Ad placeholder (leaderboard)

The Google Encoded Polyline format is a clever way to pack a long list of coordinates into a short string of printable characters. It is the format returned by the Google Directions and Roads APIs and accepted by the Static Maps API. This free tool encodes and decodes those strings in both directions, fully client-side.

How it works

The algorithm encodes each coordinate as a signed offset from the previous one, so a smooth route compresses well:

  1. Multiply the latitude and longitude by 10^precision (usually 10^5) and round to an integer.
  2. Take the difference from the previous point’s integer value.
  3. Left-shift the signed value by one bit; if the original was negative, invert all the bits (this is a one-bit zig-zag encoding of the sign).
  4. Break the result into 5-bit chunks, least significant first. Every chunk except the last is OR-ed with 0x20 to flag “more chunks follow”.
  5. Add 63 to each chunk and output it as an ASCII character.

Decoding reverses every step: read characters, subtract 63, strip the continuation bit, reassemble the 5-bit groups, undo the zig-zag, and accumulate the running latitude and longitude.

Tips and example

The two points 38.5,-120.2 and 40.7,-120.95 (precision 5) encode to the classic example string `_p~iF~ps|U_ulLnnqC from Google’s documentation.

  • Match the precision on both ends. Google Directions uses 5; OSRM and some Roads responses use 6.
  • Encoding rounds to your chosen precision, so a tiny round-trip difference is expected and harmless for maps.
  • The longer your route, the bigger the win — deltas between nearby points use very few characters.
Ad placeholder (rectangle)