mathieu revised this gist . Go to revision
1 file changed, 0 insertions, 0 deletions
gistfile1.txt renamed to get_place_id.py
File renamed without changes
mathieu revised this gist . Go to revision
1 file changed, 34 insertions
gistfile1.txt(file created)
@@ -0,0 +1,34 @@ | |||
1 | + | import requests | |
2 | + | ||
3 | + | def get_place_id(api_key, query): | |
4 | + | # Google Places API endpoint | |
5 | + | endpoint = "https://maps.googleapis.com/maps/api/place/findplacefromtext/json" | |
6 | + | ||
7 | + | # Parameters | |
8 | + | params = { | |
9 | + | "input": query, # Input can be the name or address | |
10 | + | "inputtype": "textquery", | |
11 | + | "fields": "place_id", # Request only the `place_id` field | |
12 | + | "key": api_key | |
13 | + | } | |
14 | + | ||
15 | + | # API Call | |
16 | + | response = requests.get(endpoint, params=params) | |
17 | + | ||
18 | + | # Check if the request was successful | |
19 | + | if response.status_code == 200: | |
20 | + | data = response.json() | |
21 | + | if "candidates" in data and len(data["candidates"]) > 0: | |
22 | + | return data["candidates"][0]["place_id"] # Return the first candidate's place_id | |
23 | + | else: | |
24 | + | return f"No results found for query: {query}" | |
25 | + | else: | |
26 | + | return f"Error: {response.status_code} - {response.text}" | |
27 | + | ||
28 | + | # Example usage | |
29 | + | if __name__ == "__main__": | |
30 | + | # Replace with your API key | |
31 | + | YOUR_API_KEY = "your_google_api_key" | |
32 | + | QUERY = "Le Bernardin, New York, NY" | |
33 | + | place_id = get_place_id(YOUR_API_KEY, QUERY) | |
34 | + | print(f"Place ID: {place_id}") |