python - 为什么 Google 会返回“无”坐标
问题描述
所以我正在尝试获取格拉斯哥的坐标,并且我正在尝试使用谷歌地图 API。但是,这些函数返回 None 和 None 作为坐标,我不太明白为什么。我假设我在某处有错误的参数。代码如下。
import requests
api_key = api key
google_api_key = api key
def get_coordinates(api_key, address, verbose=False):
try:
url = 'https://maps.googleapis.com/maps/api/geocode/json?key={}&address={}'.format(api_key, address)
response = requests.get(url).json()
if verbose:
print('Google Maps API JSON result =>', response)
results = response['results']
geographical_data = results[0]['geometry']['location'] # get geographical coordinates
lat = geographical_data['lat']
lon = geographical_data['lng']
return [lat, lon]
except:
return [None, None]
address = 'Glasgow, United Kingdom'
glasgow_centre = get_coordinates(google_api_key, address)
print('Coordinate of {}: {}'.format(address, glasgow_centre))
解决方案
发生索引错误是因为您没有任何结果。
记录响应以查看您收到的错误消息。这就是发生在我身上的事情:
{'error_message': '此 API 项目无权使用此 API。', 'results': [], 'status': 'REQUEST_DENIED'}
推荐阅读
- javascript - SharePoint 的客户端分类选择器
- extentreports - ExtentReports: detachReporter() 方法
- laravel - 迁移时添加多行
- javascript - 对工作日数组进行排序(JavaScript)
- gcov - 将 lcov 与 gcc-8 一起使用
- java - 如何为 Set Map of Sets 的 Java Map 声明 TypeScript 类型?
- nservicebus - AzureStorage 上的 NServiceBus 调度消息花费的时间超过了可见性超时
- sql-server - Serilog.Sinks.MSSqlServer 不填充自定义列
- java - 如何在改造中获得 Array 的响应?
- c - 检查字符串是否包含相同数量的相同字符