try语句使用googlemaps1.0.2

2024-09-30 16:41:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是python模块googlemaps1.0.2,不能在用户输入未知地址的实例中使用try语句。在

我收到的错误消息如下:

GoogleMapsError: Error 602: G_GEO_UNKNOWN_ADDRESS

我的方法如下所示(没有API密钥):

^{pr2}$

我试过:

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

谢谢


Tags: 模块实例方法用户消息address地址错误
1条回答
网友
1楼 · 发布于 2024-09-30 16:41:33

你可能还没有导入谷歌地图.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

相关问题 更多 >