Google Places API:如何从地址获取有效的place_id?
Alright, let's walk through exactly how to turn any physical address (like your example 48 Pirrama Rd, Pyrmont NSW) into a valid place_id for the Google Places API—since that's the key to unlocking all those detailed place details you referenced in the Place Docs.
First: Get the place_id for your specific address
You have two reliable ways to do this, both using Google's Maps Platform APIs:
Option 1: Use the Place Text Search API (Places-focused)
This is the most direct path if you're already working with the Places API ecosystem. Here's the request you'd send (swap YOUR_API_KEY with your actual API key):
https://maps.googleapis.com/maps/api/place/textsearch/json?query=48+Pirrama+Rd%2C+Pyrmont+NSW&key=YOUR_API_KEY
When you run this, the JSON response will have a results array. The top matching result will include the place_id you need—for your example, it's exactly ChIJN1t_tDeuEmsRUsoyG83frY4 (the same one from the Place Details example you found).
Option 2: Use the Geocoding API (Great for address + coordinates)
If you also need latitude/longitude alongside the place_id, the Geocoding API is perfect. The request structure is:
https://maps.googleapis.com/maps/api/geocode/json?address=48+Pirrama+Rd%2C+Pyrmont+NSW&key=YOUR_API_KEY
Same deal here—dig into the results array, and you'll find the same valid place_id for your address.
How to Guarantee a "Valid" place_id Every Time
A valid place_id is one that the Place Details API recognizes and returns data for. Follow these tips to avoid issues:
- Be specific with addresses: Include as much detail as possible (street number, name, city, postcode, country) to cut down on ambiguous results. For your example, adding the postcode
2009and countryAustraliamakes the request even more precise. - Always URL-encode your address: Replace spaces with
+or%20, and commas with%2C—otherwise, the API might misinterpret your request. Most programming languages have built-in functions for this (likeencodeURIComponent()in JS orurllib.parse.quote()in Python). - Validate the result type: Check the
typesfield in the response to make sure it matches what you're targeting. For a specific building, look forstreet_address; for a neighborhood,locality, etc. This helps you pick the right entry if there are multiple matches. - Use Place Autocomplete for user input: If you're building a UI where users type addresses, integrate Place Autocomplete. It returns real-time
place_idvalues as users type, so you get a valid ID immediately without manual address parsing.
Quick Recap for the Place Details API
Once you have your valid place_id, plug it into the Place Details request like the example you saw:
https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJN1t_tDeuEmsRUsoyG83frY4&key=YOUR_API_KEY
This will spit out all the detailed place data you're looking for.
内容的提问来源于stack exchange,提问作者sherifffruitfly




