-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgmaps_api.py
More file actions
107 lines (87 loc) · 3.51 KB
/
gmaps_api.py
File metadata and controls
107 lines (87 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
## From user get the place ID, from there use the search radius to look for all the mental health facitilities
# Depending on the categories
# We can offer, physciastrist, hospital, clubs (pottery,cycling,swimming,music), parks to go on walks
#GMAPS API CLIENT
from urllib.parse import urlencode
import requests
import json
GOOGLE_API_KEY = 'AIzaSyBdPwSklxcsSD-QaExwKkCp2CgkxpHcoQU'
class GoogleMapsClient(object):
lat = None
lng = None
data_type = 'json'
location_query = None
api_key = None
def __init__(self, api_key=None, address=None):
if api_key == None:
raise Exception("API key is required")
self.api_key = api_key
self.location_query = address
if self.location_query != None:
self.extract_lat_lng()
def extract_lat_lng(self, location=None):
loc_query = self.location_query
if location != None:
loc_query = location
endpoint = f"https://maps.googleapis.com/maps/api/geocode/{self.data_type}"
params = {"address": loc_query, "key": self.api_key}
url_params = urlencode(params)
url = f"{endpoint}?{url_params}"
r = requests.get(url)
if r.status_code not in range(200, 299):
return {}
latlng = {}
try:
latlng = r.json()['results'][0]['geometry']['location']
except:
pass
lat,lng = latlng.get("lat"), latlng.get("lng")
self.lat = lat
self.lng = lng
return lat, lng
def search(self, keyword, radius, location=None):
lat, lng = self.lat, self.lng
if location != None:
lat, lng = self.extract_lat_lng(location=location)
endpoint = f"https://maps.googleapis.com/maps/api/place/nearbysearch/{self.data_type}"
params = {
"key": self.api_key,
"location": f"{lat},{lng}",
"rankby":"distance",
"keyword": keyword
}
params_encoded = urlencode(params)
places_url = f"{endpoint}?{params_encoded}"
r = requests.get(places_url)
if r.status_code not in range(200, 299):
return {}
return r.json()
def distance_time(self, curr_location,des_location):
curr_loc = self.location_query
endpoint = f"https://maps.googleapis.com/maps/api/distancematrix/{self.data_type}"
params = {
"key": self.api_key,
"origins": curr_location,
"units":"metric",
"destinations": des_location
}
params_encoded = urlencode(params)
places_url = f"{endpoint}?{params_encoded}"
r = requests.get(places_url)
if r.status_code not in range(200, 299):
return {}
return r.json()
# USER_ADRESS = 'M1P4Z3'
# USER_PLACE = 'psychiatrist'
# client = GoogleMapsClient(api_key=GOOGLE_API_KEY, address=USER_ADRESS)
# #aaresults = client.search(keyword="parks", radius=10000)['results'][0]['name']
# testing = client.search(keyword=USER_PLACE, radius=10000)['results']
# potential_places = []
# dummy = []
# for i in range(len(testing)):
# potential_places.append(testing[i]['name'])
# dummy.append(testing[i]['place_id'])
# print(potential_places)
# dist = client.distance_time(curr_location =USER_ADRESS,des_location=potential_places[0] )
# bins = {"Level 1":["parks","movies"],"Level 2": ["swimming"],"Level 3":["mental health"]}
# # Currently the rating of the locations is not considered, something to look at to ensure quality services/expierences