Skip to content

Instantly share code, notes, and snippets.

@Markbnj
Created August 11, 2015 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Markbnj/df5030ce659e83f1e171 to your computer and use it in GitHub Desktop.
Save Markbnj/df5030ce659e83f1e171 to your computer and use it in GitHub Desktop.
A simple google places wrapper using requests
import requests
import json
GOOGLE_API_KEY = "<A google API key>"
TEXT_SEARCH_TEMPLATE = "https://maps.googleapis.com/maps/api/place/textsearch/json?key={}&query={}"
NEARBY_SEARCH_TEMPLATE = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key={}&location={},{}&radius={}"
DETAIL_SEARCH_ID_TEMPLATE = "https://maps.googleapis.com/maps/api/place/details/json?key={}&placeid={}"
DETAIL_SEARCH_REF_TEMPLATE = "https://maps.googleapis.com/maps/api/place/details/json?key={}&reference={}"
class GooglePlaces(object):
def __init__(self, api_key=GOOGLE_API_KEY):
self.__api_key = api_key
def search_places(self, query):
url = TEXT_SEARCH_TEMPLATE.format(self.__api_key, query)
response = requests.get(url)
results = json.loads(response.text)
return results
def search_nearby(self, latitude, longitude, radius, name=None):
url = NEARBY_SEARCH_TEMPLATE.format(self.__api_key, latitude, longitude, radius)
if name:
url = url + "&name={}".format(name)
response = requests.get(url)
results = json.loads(response.text)
return results
def get_details_by_placeid(self, place_id):
url = DETAIL_SEARCH_ID_TEMPLATE.format(self.__api_key, place_id)
response = requests.get(url)
results = json.loads(response.text)
return results
def get_details_by_reference(self, reference):
url = DETAIL_SEARCH_REF_TEMPLATE.format(self.__api_key, reference)
response = requests.get(url)
results = json.loads(response.text)
return results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment