{"id":361,"date":"2008-08-02T15:32:39","date_gmt":"2008-08-02T20:32:39","guid":{"rendered":"http:\/\/unitstep.net\/?p=361"},"modified":"2009-02-28T20:57:00","modified_gmt":"2009-03-01T01:57:00","slug":"decoding-google-maps-encoded-polylines-using-php","status":"publish","type":"post","link":"https:\/\/unitstep.net\/blog\/2008\/08\/02\/decoding-google-maps-encoded-polylines-using-php\/","title":{"rendered":"Decoding Google Maps Encoded Polylines using PHP"},"content":{"rendered":"

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

This made it somewhat tedious to decode them, as the only way to get the original list of points was to create a GPolyline<\/code><\/a> 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.<\/p>\n

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

\nGoogle Maps Polyline Decoder in PHP<\/acronym><\/a>\n<\/div>\n

<\/p>\n

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<\/a>, 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)<\/p>\n

Thankfully the polyline decoding algorithm was already available<\/a> at Mark McClure’s site. I spent a few minutes understanding the process and porting it over to PHP<\/acronym>. 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<\/acronym> functions to convert characters to their ASCII code, since PHP<\/acronym> doesn’t have a charCodeAt()<\/code> function.<\/p>\n

Please let me know if you find it to be useful.<\/p>\n

Projects using this<\/h4>\n