1

我正在使用 python 模块 googlemaps 1.0.2,在用户输入未知地址的情况下,我无法使用 try 语句。

我收到的错误消息如下所示:

GoogleMapsError: Error 602: G_GEO_UNKNOWN_ADDRESS

我的方法看起来像这样,(没有我的 API 密钥):

from googlemaps import GoogleMaps
def get_distance(address, destination):
    try:
        gmaps = GoogleMaps(api_key)
        directions = gmaps.directions(address,destination)
        distance = directions['Directions']['Distance']['meters']/1600.0
        return distance
    except ???:

我试过了:

except GoogleMapsError:
except 'GoogleMapsError: Error 602: G_GEO_UNKNOWN_ADDRESS'

谢谢

4

1 回答 1

2

您可能尚未导入 googlemaps.GoogleMapsError 异常。

from googlemaps import GoogleMaps, GoogleMapsError

def get_distance(address, destination):
    try:
        gmaps = GoogleMaps(api_key)
        directions = gmaps.directions(address,destination)
        distance = directions['Directions']['Distance']['meters']/1600.0
        return distance
    except GoogleMapsError:
        pass
于 2012-10-30T01:08:03.360 回答