-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_trail.py
46 lines (40 loc) · 1.75 KB
/
map_trail.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
44
45
46
import googlemaps
from config import directions_api_key
import json
import urllib.request
# gmaps = googlemaps.Client(directions_api_key)
GEOCODE_BASE_URL = "https://maps.googleapis.com/maps/api/geocode/json"
DIRECTIONS_BASE_URL = "https://www.google.com/maps/dir/"
api = 1
# Geocoding an address
def get_geocode(address):
'''Returns a geocode for an address, which is a json that contains
multiple values, including latitude and longitude'''
params = urllib.parse.urlencode({"address": address, "key": directions_api_key,})
url = f"{GEOCODE_BASE_URL}?{params}"
result = json.load(urllib.request.urlopen(url))
if result['status'] in ['OK', 'ZERO_RESULTS']:
return result['results']
raise Exception(result['error_message'])
def get_lat_long(address):
'''Returns the latitude and longitude from an address'''
geocode_result = get_geocode(address)
if len(geocode_result) > 0:
latitude = geocode_result[0]['geometry']['location']['lat']
longitude = geocode_result[0]['geometry']['location']['lng']
else:
return False, False
return latitude, longitude
def get_string(latitude, longitude):
'''Concatenates latitude and longitude into string needed for
directions url'''
string_result = str(latitude) + ',' + str(longitude)
return string_result
def get_directions_url_trails(origin_lat, origin_long, destination_lat, destination_long):
'''Creates directions url using origin and destination longitude
and latitudes'''
origin = get_string(origin_lat, origin_long)
destination = get_string(destination_lat, destination_long)
params = urllib.parse.urlencode({"api": api, "origin": origin, "destination": destination,})
url = f"{DIRECTIONS_BASE_URL}?{params}"
return url