2012-10-30 27 views
1

我正在使用python模塊googlemaps 1.0.2,並且我無法在用戶輸入未知地址的實例中使用try語句。嘗試使用GoogleMaps語句1.0.2

,我得到看起來像這樣的錯誤消息:

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' 

謝謝

+1

'from googlemaps import GoogleMaps,GoogleMapsError' ??? ??? – John

回答

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