Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code for Google Place API Demo: https://youtu.be/kgWaSZU0Um8
import requests
import json
import time
key = 'YOURKEYHERE'
url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json'
all_data = {}
search_lat_long = ['40.752933731021514, -73.98547317732873','40.75036387669075, -73.98316281312741']
for coords in search_lat_long:
params = {
'key' : key,
'location' : coords,
'fields' : 'name,business_status,formatted_address,geometry,types,place_id',
'radius' : '500',
'type' : 'restaurant'
}
r = requests.get(url, params=params)
data = json.loads(r.text)
for restaurant in data['results']:
all_data[restaurant['place_id']] = restaurant
if 'next_page_token' in data:
time.sleep(5)
print('---------')
new_params = {
'key' : key,
'pagetoken' : data['next_page_token']
}
r = requests.get(url, params=new_params)
data = json.loads(r.text)
for restaurant in data['results']:
all_data[restaurant['place_id']] = restaurant
if 'next_page_token' in data:
time.sleep(5)
print('---------')
new_params = {
'key' : key,
'pagetoken' : data['next_page_token']
}
r = requests.get(url, params=new_params)
data = json.loads(r.text)
for restaurant in data['results']:
all_data[restaurant['place_id']] = restaurant
json.dump(all_data,open('places.json','w'),indent=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment