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

调用Google Maps Python API的places_nearby函数时出现API错误

Fixing the places_nearby API Error in Google Maps Python Client

Hey there! Let's get your places_nearby call working smoothly. The most likely culprit here is missing required parameters—this function can't run with just a location value. Let's break down the fixes and other key checks:

First: Add Required Parameters

The places_nearby method needs one of two parameter combinations to work:

  1. A radius (in meters) to define your search area
  2. rankby='distance' plus at least one filter like keyword, name, or type (to sort results by distance from your location)

Example 1: Using radius (Simplest Fix)

If you just want to search within a specific geographic area:

import googlemaps

gmaps = googlemaps.Client(key=maps_api_key)
# Search within 1000 meters of the NYC coordinates provided
places_result = gmaps.places_nearby(
    location=(40.714224, -73.961452),
    radius=1000
)

Example 2: Using rankby='distance'

If you want results sorted by proximity (instead of a fixed radius), pair it with a filter:

places_result = gmaps.places_nearby(
    location=(40.714224, -73.961452),
    rankby='distance',
    keyword='coffee'  # Filter results to coffee shops
)

Other Critical Checks

  • Verify API Key Permissions:
    Log into your Google Cloud Console and confirm:
    • The Places API is enabled for your project
    • Your API key is linked to a valid billing account (most Google Maps APIs require active billing to avoid access denial errors)
  • Catch Detailed Error Messages:
    Add exception handling to see exactly what the API is complaining about—this will speed up debugging:
    try:
        places_result = gmaps.places_nearby(location=(40.714224, -73.961452))
    except googlemaps.exceptions.ApiError as e:
        print(f"Full API Error Details: {e}")
    
    Common errors here include MISSING_PARAMETER (confirming the missing radius/rankby issue) or REQUEST_DENIED (pointing to a permissions/billing problem).
  • Double-Check Coordinates:
    Your location format (latitude first, longitude second) is correct, and the NYC coordinates you're using look valid—just a quick sanity check to rule out silly typos!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:38:44