Skip to content

Instantly share code, notes, and snippets.

@thisismattmiller
Created November 20, 2020 23:05
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 thisismattmiller/d01a8f1da462293f0aa31aac27e56f55 to your computer and use it in GitHub Desktop.
Save thisismattmiller/d01a8f1da462293f0aa31aac27e56f55 to your computer and use it in GitHub Desktop.
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