Python | Get a set of places according to search query using Google Places API

Python | Get a set of places according to search query using Google Places API

To get a set of places according to a search query using the Google Places API, you'd typically follow these steps:

  • Get an API key: You need to get an API key from the Google Cloud Console. Ensure that the Places API is enabled for the project linked to your API key.

  • Install Required Libraries: For this example, we'll use the requests library to make HTTP requests to the Google Places API.

pip install requests 
  • Make an API Request:

Here's a Python function to fetch places based on a search query using the Google Places API:

import requests def get_places(query, api_key): # Define the endpoint URL (Nearby Search request) ENDPOINT = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?' # Define the parameters location = '37.4219999,-122.0840575' # example location: Googleplex, CA; you should replace this with your desired location radius = 5000 # in meters params = { 'location': location, 'radius': radius, 'keyword': query, 'key': api_key } # Make the request and get the response res = requests.get(ENDPOINT, params=params).json() # Return the set of places return res['results'] # Use the function api_key = 'YOUR_API_KEY_HERE' places = get_places('coffee', api_key) for place in places: print(place['name']) 

Note:

  • Replace 'YOUR_API_KEY_HERE' with your actual Google Places API key.
  • Adjust the location and radius parameters as needed. The provided values are just an example.
  • The Places API may have costs associated with extensive use. Always monitor your usage to avoid unexpected charges.

More Tags

kendo-listview show-hide flutter-packages exceldatareader coordinate jsch spring-data-elasticsearch silent python-telegram-bot max

More Programming Guides

Other Guides

More Programming Examples