Decoding Google Maps Encoded Polylines using PHP

I’ve talked about the Google Maps encoded polyline format before. While there’s some nice utilities for encoding polylines that take the work out of implementing it yourself, I couldn’t find many polyline decoders.

This made it somewhat tedious to decode them, as the only way to get the original list of points was to create a GPolyline and then pull out the points from that object. This is not ideal since the work must always be done on the client side with JavaScript and using Google Maps.

To solve this, I quickly ported the algorithm over to PHP from the JavaScript source. Please feel free to download/modify/use this script.

Since the encoded polyline format offers numerous benefits (and because I had data already stored in this format) I did not want to move away from it. At the same time, I needed access to the points for working with things like Google Static Maps, which curiously does not accept the encoded polyline format for displaying paths. (Probably to reduce resource usage on their end, since decoding takes CPU time)

Thankfully the polyline decoding algorithm was already available at Mark McClure’s site. I spent a few minutes understanding the process and porting it over to PHP. The source is attached above and is released under an MIT license. Basically, the only change I had to make was to use some PHP functions to convert characters to their ASCII code, since PHP doesn’t have a charCodeAt() function.

Please let me know if you find it to be useful.

Projects using this

27 Comments »

  1. dud, how i use this script? what variable do i parse

    grettings from chile

  2. The function is for taking an encoded polyline; it then returns an array of the points that make up the polyline.

    The polyline is an encoded string according to Google’s polyline algorithm.

    This is not a standalone script, it is merely a function that you can use in your own application.

  3. I tested the script and the array looks fine! I’m going to use it, thanks!

  4. @Willem
    Thanks for your support! If you create a project with it, let me know!

  5. Thanks for this.
    P.S. Mark McClure is the actual code being used on google maps.

  6. […] contains a weird data that seems to be encoded. After some research i found Peter Chng’s blog post that points a php script that decodes encoded polyline data. I’d tried to port the script to […]

  7. many thanks Peter

  8. […] which contains a weird data that seems to be encoded. After some research i found Peter Chng’s blog post that points a php script that decodes encoded polyline data. I’d tried to port the script to […]

  9. It sounds great, I will test it but I must add the Zoom Levels string decoding too…

    Can I post the code when I’ll end it and if you like it.

  10. Thanks guys, after i try your application, i want discuss with you..

  11. i mean Google Maps Polyline Decoder in PHP

  12. Nice! You are a lifesaver. Thanks (:

  13. is there encoder?

  14. I’m not sure but try this polyline encoder http://code.pulpmedia.at/php/polyline_encoder/

  15. thank you very much!

  16. […] which contains a weird data that seems to be encoded. After some research i found Peter Chng’s blog post that points a php script that decodes encoded polyline data. I’d tried to port the script to […]

  17. Great contribution man!

  18. Absolutely wonderful! You made short work of what would have taken me all day to figure out.

  19. […] points in that file, decode these points with a php decoder ( php decoder obtained here: http://unitstep.net/blog/2008/08/02/decoding-google-maps-encoded-polylines-using-php/ ) included from another file, then create boxes to define the region near the route between the […]

  20. Thanks a lot for this.

  21. Hi. Trying out your code. Having some trouble with it. Having some Google polyline saved in a mysql. Works great when working on a map.

    When serverside running your script the latitude is correct but the longitude shows wrong value.

    Any suggestions?

  22. I tried the code in http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/decode.html

    Gives same result as your PHP, but this is also wrong value for longitude. When loading the string on a google map I get correct lng value.

    Weird.

  23. FYI, Google static maps now support encoded polyline, in “path” parameter

  24. Works perfect for what I needed and will be implemented in the project at the URL above.

    Thank you so much for the work on this. I was digging through the encoding information starting to work it out backwards to decode it when I thought, “Someone has to have done this already!” And sure enough, you have! Thank you.

  25. Thanks a lot for this function! You want a beer? 🙂

  26. Thank you !

  27. Thanks, you saved me.

Post a Comment

(required)

(will not be published) (required)

XHTML tags allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Note: rel="nofollow" will be added to all links in comments.

Comments will be closed on Saturday, March 18th, 2017.