google-mapsgoogle-elevation-api

Google Elevation API wrong path parameter


I'm using google elevation service

I want to fetch data for my path, which consists of about 1k points. I know that I cannot sent them all in query divided by | separator, because I would exceed request size limit, so I'm encoding those points as it is mentioned in docs by Encoded Polyline Algorithm and send them like:

https://maps.googleapis.com/maps/api/elevation/json?key=KEY&path=cgg%7EE%7C%7B%7EpUfcaAwe%7CG&samples=3

Points: 36.578581,-118.291994|36.23998,-116.83171

Points encoded: cgg~E|{~pUfcaAwe|G

Points encoded + urlencoded: cgg%7EE%7C%7B%7EpUfcaAwe%7CG

But I'm getting the following error message:

Invalid request. Invalid 'path' parameter..

When I ask google with non encoded query (path separated by pipe separator) I receive proper data.

Do you have some hints what I am doing wrong in here?


Solution

  • The path parameter expects your encoded points to be prefixed with enc:

    This is not super clear from the documentation, I must admit.

    With your encoded points cgg~E|{~pUfcaAwe|G the path parameter would become:

    path=enc:cgg~E|{~pUfcaAwe|G

    Proof of concept:

    https://maps.googleapis.com/maps/api/elevation/json?key=YOUR_API_KEY&path=enc:cgg~E|{~pUfcaAwe|G&samples=3

    You must replace YOUR_API_KEY with your own key.

    This returns the following:

    {
       "results" : [
          {
             "elevation" : 4411.8828125,
             "location" : {
                "lat" : 36.57858,
                "lng" : -118.29199
             },
             "resolution" : 19.08790397644043
          },
          {
             "elevation" : 1372.885498046875,
             "location" : {
                "lat" : 36.41150237848279,
                "lng" : -117.5602587614725
             },
             "resolution" : 9.543951988220215
          },
          {
             "elevation" : -84.51690673828125,
             "location" : {
                "lat" : 36.23998,
                "lng" : -116.83171
             },
             "resolution" : 9.543951988220215
          }
       ],
       "status" : "OK"
    }