|
| 1 | +# Step 2: Get a Google Places API Key |
| 2 | +# Go to the Google Cloud Console |
| 3 | + |
| 4 | +# Create a project (if you don’t already have one) |
| 5 | + |
| 6 | +# Enable the following APIs: |
| 7 | + |
| 8 | +# Places API |
| 9 | + |
| 10 | +# Geocoding API (optional, if using lat/lng) |
| 11 | + |
| 12 | +# Generate an API Key (and restrict it to prevent misuse) |
| 13 | + |
| 14 | +# Step 3: Use Python and the Google Places API |
| 15 | +# If you have place names or coordinates, you can retrieve full details like address, phone, etc. Here's an example using Python: |
| 16 | + |
| 17 | +import requests |
| 18 | + |
| 19 | +API_KEY = 'YOUR_GOOGLE_MAPS_API_KEY' |
| 20 | +places = ['Starbucks, San Francisco, CA', 'Museum of Modern Art, NY'] |
| 21 | + |
| 22 | +def get_place_details(place_name): |
| 23 | + # Search for the place to get the Place ID |
| 24 | + search_url = "https://maps.googleapis.com/maps/api/place/findplacefromtext/json" |
| 25 | + params = { |
| 26 | + 'input': place_name, |
| 27 | + 'inputtype': 'textquery', |
| 28 | + 'fields': 'place_id', |
| 29 | + 'key': API_KEY |
| 30 | + } |
| 31 | + response = requests.get(search_url, params=params).json() |
| 32 | + candidates = response.get('candidates') |
| 33 | + |
| 34 | + if not candidates: |
| 35 | + return None |
| 36 | + |
| 37 | + place_id = candidates[0]['place_id'] |
| 38 | + |
| 39 | + # Use the Place ID to get full details |
| 40 | + details_url = "https://maps.googleapis.com/maps/api/place/details/json" |
| 41 | + details_params = { |
| 42 | + 'place_id': place_id, |
| 43 | + 'fields': 'name,formatted_address,formatted_phone_number,website', |
| 44 | + 'key': API_KEY |
| 45 | + } |
| 46 | + details_response = requests.get(details_url, params=details_params).json() |
| 47 | + return details_response.get('result') |
| 48 | + |
| 49 | +# Example usage |
| 50 | +for place in places: |
| 51 | + details = get_place_details(place) |
| 52 | + print(details) |
| 53 | + |
| 54 | +# Step 4: If You Have Coordinates |
| 55 | + |
| 56 | +def reverse_geocode(lat, lng): |
| 57 | + url = f"https://maps.googleapis.com/maps/api/geocode/json" |
| 58 | + params = { |
| 59 | + 'latlng': f"{lat},{lng}", |
| 60 | + 'key': API_KEY |
| 61 | + } |
| 62 | + response = requests.get(url, params=params).json() |
| 63 | + return response['results'][0] if response['results'] else None |
| 64 | + |
| 65 | +#################################################### |
| 66 | + |
| 67 | +Step-by-Step Instructions |
| 68 | +1. Go to Google Cloud Console |
| 69 | +Visit: https://console.cloud.google.com/ |
| 70 | + |
| 71 | +2. Create or Select a Project |
| 72 | +At the top, click the project dropdown. |
| 73 | + |
| 74 | +Click “New Project” or select an existing one. |
| 75 | + |
| 76 | +3. Enable the Places API |
| 77 | +In the left sidebar, go to “APIs & Services” > “Library”. |
| 78 | + |
| 79 | +Search for “Places API”. |
| 80 | + |
| 81 | +Click it, then click “Enable”. |
| 82 | + |
| 83 | +4. Create API Credentials |
| 84 | +Go to “APIs & Services” > “Credentials”. |
| 85 | + |
| 86 | +Click “Create Credentials” > “API Key”. |
| 87 | + |
| 88 | +A new API key will be generated. |
| 89 | + |
| 90 | +5. Restrict Your API Key (Recommended) |
| 91 | +In the Credentials page, click on your new API key. |
| 92 | + |
| 93 | +Under “Application Restrictions”, choose: |
| 94 | + |
| 95 | +HTTP referrers (websites), |
| 96 | + |
| 97 | +IP addresses (servers/scripts), |
| 98 | + |
| 99 | +or Android/iOS apps, depending on your use. |
| 100 | + |
| 101 | +Under “API Restrictions”, select “Restrict key”, then choose: |
| 102 | + |
| 103 | +✅ Places API |
| 104 | + |
| 105 | +6. Save and Use Your Key |
| 106 | +Click Save. |
| 107 | + |
| 108 | +Use your API key in your application like this: |
| 109 | + |
| 110 | +python |
| 111 | +Copy |
| 112 | +Edit |
| 113 | +https://maps.googleapis.com/maps/api/place/details/json?place_id=PLACE_ID&key=YOUR_API_KEY |
| 114 | +🧠 Tips |
| 115 | +Billing is required, but Google gives $200/month free for Places API usage. |
| 116 | + |
| 117 | +You can monitor your usage in the “Billing” section of your project. |
| 118 | + |
| 119 | +Would you like example Python code using the key? |
0 commit comments