You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Google Maps API调用时出现的HTTP 400错误?

问题

尝试计算起点元组列表中每个起点到终点元组列表中每个终点的驾车时间时,运行代码出现HTTPError: HTTP Error: 400错误,代码如下:

import googlemaps
code = 'xxx'
gmaps = googlemaps.Client(key = code)
groceryLocations = [(lat, lon) for lat, lon in zip(grocery['SnapY'],
                                               grocery['SnapX'])]
startingPoints = [(float(lat), float(lon)) for lat, lon in zip(zipCentroid['INTPTLAT20'],
                                             zipCentroid['INTPTLON20'])]
origins = startingPoints
destinations = groceryLocations
now = datetime.now()
dist_matrix = gmaps.distance_matrix(origins, destinations, mode='driving', departure_time=now)

排查与解决思路

  • 确认API密钥状态:检查code对应的Google Maps API密钥是否已启用距离矩阵API,同时确认密钥未超出调用配额、未设置IP访问限制。
  • 验证坐标合法性:
    • 检查groceryLocations和startingPoints中的经纬度值是否在合法范围(纬度-9090,经度-180180),排除无效数值。
    • 确认坐标顺序:Google Maps API要求坐标格式为(纬度, 经度),需确保grocery['SnapY']、zipCentroid['INTPTLAT20']确实对应纬度,SnapX、INTPTLON20对应经度。
  • 修正departure_time参数:API要求传入UTC时间戳或UTC datetime对象,本地时间可能导致参数错误,可修改为:
    import datetime
    now = datetime.datetime.utcnow()
    
    或直接传入Unix时间戳:
    now = int(datetime.datetime.now().timestamp())
    
  • 控制请求规模:距离矩阵API对单次请求的起点、终点数量有上限(免费版通常为100×100),若列表过长,需拆分请求分批调用。
  • 检查网络环境:如果运行环境使用代理,需为googlemaps客户端配置正确的代理参数,避免请求被拦截。

内容的提问来源于stack exchange,提问作者bsilver52

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 03:42:38