How to decode Polylines from Google Maps Direction API in PHP

Shehzad Bilal picture Shehzad Bilal · Mar 13, 2013 · Viewed 13.2k times · Source

I have json response from the following URL:

http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los%20Angeles,CA&sensor=false

JSON path routes[x].legs[y].steps[z].polyline.points:

"azq~Fhc{uOAlB?jB?^?P?P?B@V@|J@fA?xA@xA?h@?B?F?@?tA@xD?h@BnA@|A@rB@f@?d@@v@AxB?d@AZEzA?BIjB?@Cx@?@EzAAlC?F?F?T?B?f@DhHBhD?@?R?l@?R?|CCpDAj@E|DGnDKzCCb@OtBa@rFGfAAb@?@?FAp@?ADbJD|F@bF@@@fERhd@BrEFdDBtBAvBAx@@l@?n@@^@bANnQ?rABnM?jC?hH@fA?@BF?vC?hB?@BpM?@?j@@p@@|KB~Q@pFBbHBvF@z@?f@@jB?nA@z@DzD@VJ~CLfC\|E?B?@HnANtAVpDRpCLbB^dFTxC@LZvDF^HrALlCHEB|H?DBpEB~V?^BhDJR?@@\?~A?nABrL?@?jD@vD@vA?h@?BLx[?x@?B?\?F@pA?h@D~H?@Bz@Dr@RbCLfA\rBPv@@@T~@t@bCPf@z@xBd@rAf@dB\zAN~@PjAT~BFrADxAHX?z@?@HfW?x@?F?@@dD@^F|Y@v@D|JBzH?rB@tAApABxB?bA@dBBxABlAJ~CJrBDfANhBNjCLlCLpBHlBFnB@C?|A?v@AlBCdA?r@EjEC|BItEMdGEtAIfEI|BKzDOzGEjCCl@?@MnDWHSrFSlFAd@?@qA|[Ct@Cj@At@AbA?hBAdBClBQjFQnECr@EAYjFIzAWxDQpCYpEAFItACt@S~C]|GSlEMnCMtCGdAKlBQxDg@bLAT?BKrCAn@Ad@?x@?p@?J?|@@lA@z@BbABn@Bt@@@HnAPxB@LB^LATBPAP~@Z~ALn@?@@Fd@|BjAfGd@dDd@|D\bFDf@D~@@f@B|@@xCJP?dBBEDtE@bADlAREJlABh@Dp@F@@xEJdBHlCF~C@nA?@?@DfG?ADhLBbD@x@?F@~C?dCNbTDrIBxDLbO@~AVY?@DfHEvDGlC]fHGhD?lHPlP?@?B?R?@BfBNbRBpENfQDrGBvCDrEBtEBzABfABx@B~@^FHx@H|@@bDPxAZpCTbDNDBlC@j@@j@BhAHhLBvC?p@BlB?jAAfAAx@C@MzDM|B_@tDq@pF]fB]zAo@fCc@~Am@jBo@dBoCxG?@?@Sd@g@vAY~@St@W|@_@bBUhA_@zBWhBKAOpAKfAEp@Gz@Cb@GpACZAVAh@Ad@AX?f@At@CpB"

I want to decode the Polyline points string to lat long values returned by the above URL using PHP.

I have found some results in Java and Objective C , but I need it in PHP.

Answer

Mike Davlantes picture Mike Davlantes · Nov 6, 2015

Python Implementation

This isn't in PHP, but this thread is near the top of the search results if you're looking to decode polyline strings from Google Maps. In case anyone else needs it (much like I did), here's a Python implementation for decoding polyline strings. This is ported from the Mapbox JavaScript version; more info found on my repo page.

def decode_polyline(polyline_str):
    index, lat, lng = 0, 0, 0
    coordinates = []
    changes = {'latitude': 0, 'longitude': 0}

    # Coordinates have variable length when encoded, so just keep
    # track of whether we've hit the end of the string. In each
    # while loop iteration, a single coordinate is decoded.
    while index < len(polyline_str):
        # Gather lat/lon changes, store them in a dictionary to apply them later
        for unit in ['latitude', 'longitude']: 
            shift, result = 0, 0

            while True:
                byte = ord(polyline_str[index]) - 63
                index+=1
                result |= (byte & 0x1f) << shift
                shift += 5
                if not byte >= 0x20:
                    break

            if (result & 1):
                changes[unit] = ~(result >> 1)
            else:
                changes[unit] = (result >> 1)

        lat += changes['latitude']
        lng += changes['longitude']

        coordinates.append((lat / 100000.0, lng / 100000.0))

    return coordinates