Yu Zhang
Yu Zhang

Reputation: 443

how to handle error from google map api?

I am trying to query some direction information using the Google Map api. I want to be able to handle the NOT_FOUND situation when a non-existing location is accidentally put in. For example:

directions_result = gmaps.directions('bowcwe',
                                 'awef',
                                 waypoints = 'bjois',
                                 optimize_waypoints = True)

This returns an error message 'googlemaps.exceptions.ApiError: NOT_FOUND'. How can I make an exception to this particular scenario? Thank you very much!

Upvotes: 1

Views: 2384

Answers (1)

Brandhout
Brandhout

Reputation: 178

The errors can be accessed through googlemaps.exceptions

Example code:

import googlemaps
gmaps = googlemaps.Client(key='AIza')
try:
    location = (50, 10)
    radius = 1000
    keyword = 'bar'
    returnedPlaces = gmaps.places_radar(location=location, radius=radius, keyword=keyword)

except googlemaps.exceptions.ApiError as err :
    print('API key is invalid')

I made the key 'AIza' to pass the first validity check and get an actual API error.

Hope this helps!

Upvotes: 1

Related Questions