C implementation of Google's 'Encoded Polyline Algorithm'
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Google’s encoded polyline format compresses a list of latitude and longitude pairs into a short printable string. It is a compact transport format for routes and map overlays, and the algorithm is straightforward to implement in plain C once you understand the delta and bit-packing steps.
How the Encoding Works
Each coordinate pair goes through the same pipeline:
- Multiply latitude and longitude by
100000. - Round to integers.
- Store the difference from the previous point instead of the full value.
- Left-shift the delta by one bit.
- Invert the shifted number if the original delta was negative.
- Emit the result in 5-bit chunks with
63added to each chunk.
The delta step is what makes the format small. Adjacent route points are usually close together, so the change from one point to the next is much smaller than the absolute coordinate.
A Minimal Plain C Encoder
The following example encodes Google’s standard sample route:
Compile and run it:
Expected output:
Why the Negative-Value Step Matters
Most implementation bugs come from sign handling. The order is important:
- compute the signed delta
- left-shift it
- invert the shifted value if the original delta was negative
If you invert before shifting, or if you encode absolute values instead of deltas, the output string may still look reasonable but will decode to the wrong route.
The same caution applies to rounding. The algorithm expects scaled integers, not raw floating-point values. If you truncate when you meant to round, points near half-step boundaries can drift.
Buffer Management in C
The example uses count * 24 + 1 bytes as a simple upper-bound buffer. That is fine for a small encoder demo because each point contributes two numbers and typical encoded chunks stay short. For production code, you may prefer a dynamically growing buffer to avoid guessing.
Even in a demonstration, three C-specific details matter:
- check
malloc - null-terminate the output
- free the buffer after use
These are not part of the polyline algorithm itself, but they are part of a correct C implementation.
Testing Your Output
Polyline code is easy to get almost right. That is why testing against known vectors matters more than intuition. A good minimal test checks both the expected string and a few edge cases:
Also test negative coordinates, repeated points, and long routes. If you later add a decoder, round-trip tests are even better: encode points, decode the string, and confirm the reconstructed coordinates match the originals within the expected precision.
Common Pitfalls
Forgetting the 1e5 scaling factor is a classic mistake. Without it, the encoded route is meaningless.
Another common error is encoding absolute coordinates instead of differences from the previous point. The algorithm depends on deltas for both correctness and compactness.
Memory handling causes a separate class of bugs in C versions. Underallocating the output buffer or forgetting the terminating null byte can make the encoder look broken even when the math is correct.
Finally, do not rely on visual inspection of the output string. Polyline strings are opaque by design, so use known examples and decoder checks.
Summary
- Encoded polyline stores scaled coordinate deltas, not raw latitude and longitude values.
- The sign-transform step for negative deltas is the most error-prone part.
- A small plain C encoder can reproduce Google’s sample output exactly.
- C implementations must handle allocation, termination, and freeing explicitly.
- Verify the algorithm with known sample routes before using it in production.

