Last active
January 24, 2025 12:33
-
-
Save 0187773933/292e5327a4bab8a76816b80054f919dc to your computer and use it in GitHub Desktop.
Google Maps Location Search With Filters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import requests | |
import json | |
import time | |
API_KEY = "asdf" | |
def miles_to_meters( miles ): | |
return ( miles * 1609.34 ) | |
def get_json_request(url, params, n_pages=None): | |
results = [] | |
params["key"] = API_KEY | |
page = 1 | |
while True: | |
print(f"Getting page {page}") | |
response = requests.get(url, params=params) | |
response.raise_for_status() | |
data = response.json() | |
results.extend(data['results']) | |
next_page_token = data.get('next_page_token') | |
if next_page_token and (n_pages is None or page < n_pages): | |
time.sleep(2) | |
params['pagetoken'] = next_page_token | |
page += 1 | |
else: | |
break | |
return results | |
def get_places(location, query , n_pages=None): | |
base_url = "https://maps.googleapis.com/maps/api/place/textsearch/json" | |
params = { 'query': f"{query} in {location}" } | |
return get_json_request(base_url, params, n_pages) | |
def get_nearby_places( lat , lng , query , radius=5000 , n_pages=None ): | |
base_url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json" | |
params = { | |
'location': f"{lat},{lng}", | |
'radius': radius, | |
'keyword': query, | |
} | |
return get_json_request(base_url, params, n_pages) | |
def filter_locations(start_location, filters): | |
base_url = "https://maps.googleapis.com/maps/api/place/textsearch/json" | |
params = { | |
'query': f"{filters[0]['place']} in {start_location}", | |
# 'radius': filters[0]['radius'], | |
} | |
locations = get_json_request(base_url, params, filters[0].get('n_pages')) | |
for filter_index, x_filter in enumerate(filters[1:], start=2): | |
new_locations = [] | |
for location_index, location in enumerate(locations, start=1): | |
lat, lng = location['geometry']['location']['lat'], location['geometry']['location']['lng'] | |
print(f"Filter {filter_index}, Location {location_index}: Finding nearby {x_filter['place']}") | |
nearby_places = get_nearby_places(lat, lng, x_filter['place'], radius=x_filter['radius'], n_pages=x_filter.get('n_pages')) | |
if nearby_places: | |
for place_index, place in enumerate(nearby_places, start=1): | |
p_lat, p_lng = place['geometry']['location']['lat'], place['geometry']['location']['lng'] | |
print(f"\tNearby {x_filter['place']} {place_index}: {p_lat}, {p_lng}") | |
new_locations.append(location) | |
locations = new_locations | |
return locations | |
if __name__ == "__main__": | |
starting_point = "Ohio" | |
filters = [ | |
{'place': 'Meijer', 'radius': miles_to_meters(1), 'n_pages': 2}, | |
{'place': 'United Dairy Farmers', 'radius': miles_to_meters(2), 'n_pages': 1} , | |
{'place': 'Hardee\'s', 'radius': miles_to_meters(1), 'n_pages': 1} | |
] | |
locations = filter_locations( starting_point , filters ) | |
for location in locations: | |
print(location['name'] , "===" ,location['formatted_address']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment