-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoogleWrapper.py
43 lines (39 loc) · 1.81 KB
/
GoogleWrapper.py
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
import googlemaps
import os
from datetime import datetime
class GoogleWrapper:
def __init__(self, key=os.environ.get('GOOGLE_MAPS_API_TOKEN')):
self.gmaps = googlemaps.Client(key)
def get_distance_matrix(self, origin, destination):
now = datetime.now()
matrix = self.gmaps.distance_matrix(origin,
destination,
mode="driving",
departure_time=now,
language="de",
traffic_model = "best_guess")
distance = matrix['rows'][0]['elements'][0]['distance']
duration = matrix['rows'][0]['elements'][0]['duration']
duration_in_traffic = matrix['rows'][0]['elements'][0]['duration_in_traffic']
return {"distance": distance, "duration": duration, "duration_in_traffic": duration_in_traffic}
def get_geocode_for_location(self, location_name):
result = self.gmaps.geocode(location_name)
locations = []
if len(result) > 0:
for place in result:
location = {}
location["address"] = place["formatted_address"]
location["geocode"] = place["geometry"]["location"]
locations.append(location)
return locations
else:
result = self.gmaps.places(location_name)
if len(result["results"]) > 0:
for place in result["results"]:
location = {}
location["address"] = place["formatted_address"]
location["geocode"] = place["geometry"]["location"]
locations.append(location)
return locations
else:
return None