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 に答える