调用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:
- A
radius(in meters) to define your search area rankby='distance'plus at least one filter likekeyword,name, ortype(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:
Common errors here includetry: places_result = gmaps.places_nearby(location=(40.714224, -73.961452)) except googlemaps.exceptions.ApiError as e: print(f"Full API Error Details: {e}")MISSING_PARAMETER(confirming the missing radius/rankby issue) orREQUEST_DENIED(pointing to a permissions/billing problem). - Double-Check Coordinates:
Yourlocationformat (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
相关产品推荐
相关产品推荐

