gabestangel
gabestangel

Reputation: 11

Googlemaps elevation API giving an error when passing arguments

I'm trying to get the elevation from an excel file using pandas to read the file and jupyter notebook. I'm currently trying to figure out why my code is producing an error.

Here is what my code looks like:

import os

import googlemaps
gmaps = googlemaps.Client(key= "XXXXXXXXXXXXXXXXXXXXXX")
elevation = gmaps.elevation("40.88292", "-98.37342")
print(elevation)

Its returning the error:

  File "C:/Users/gabe/Desktop/a.py", line 6, in <module>
    elevation = gmaps.elevation("40.88292", "-98.37342")
  File "C:\Users\gabe\AppData\Local\Programs\Python\Python38\lib\site-packages\googlemaps\client.py", line 409, in wrapper
    result = func(*args, **kwargs)
TypeError: elevation() takes 2 positional arguments but 3 were given

I have tried using a ton of different variations of the coordinates as arguments but it always returns this error.

Upvotes: 0

Views: 242

Answers (1)

Derek Eden
Derek Eden

Reputation: 4628

Docs say to pass in a list of locations, which can be a list/tuple of latlon coordinates.

So likely:

gmaps.elevation([("40.88292", "-98.37342")]) 

should work (as per @AMC comments)

you could alternatively build the json request yourself following the API docs

from urllib.request import urlopen
import json
latlon = ["40.88292", "-98.37342"]
api_key = 'YOUR_API_KEY'
url = f'https://maps.googleapis.com/maps/api/elevation/json?locations={",".join(latlon)}&key={api_key}'
response = urlopen(url)
result = json.load(response)

result

{'results': [{'elevation': 568.84423828125,
   'location': {'lat': 40.88292, 'lng': -98.37342},
   'resolution': 4.771975994110107}],
 'status': 'OK'}

Upvotes: 1

Related Questions