-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaps.py
More file actions
49 lines (41 loc) · 1.78 KB
/
maps.py
File metadata and controls
49 lines (41 loc) · 1.78 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
import requests
from secret import *
API_KEY=yelpSecret
def get_longlat(place):
url='https://maps.googleapis.com/maps/api/geocode/json?'
url+='address={}&key={}'.format(place,API_KEY)
request=requests.get(url)
# print url
#check if valid place before query googlemaps
assert len(request.json()['results'])!=0, "invalid location: {}".format(place)
result=request.json()['results'][0]['geometry']['location'].values()
# print result
r =str(result[0])+', '+str(result[1])
# print place,': return longlat "R" as a STRING====', r
return r
def get_distance(longlat1,longlat2):
url='https://maps.googleapis.com/maps/api/distancematrix/json?'
url+='origins={}&destinations={}'.format(longlat1,longlat2)
# print '====== GET DISTANCE URL ', url
request=requests.get(url)
result=request.json()['rows'][0]['elements'][0]['distance']['text']
# print request.json()['rows'][0]['elements'][0]
# print '=======Distance btwn longlat1 & 2 ====', result
return result
def find_midpoint(longlat1,longlat2):
#divide distance by 2 and get longlat of that location
#input is string, convert to array of integers for math, then back to string
# print'string to array========', longlat1.split(',')
longlat1_list=longlat1.split(',')
longlat2_list=longlat2.split(',')
midx=(float(longlat1_list[0])+float(longlat2_list[0]))/2
midy=(float(longlat1_list[1])+float(longlat2_list[1]))/2
midxy=str(midx)+', '+str(midy)
# print 'MIDPOINT========= as string: ',midxy
return midxy
# # test calling
# get_d= get_distance('los angeles','40.7127837, -74.0059413')
# print 'get_d', get_d
# midxy=find_midpoint(get_longlat('los angeles'),'40.7127837, -74.0059413')
# print 'midxy', midxy
# get_longlat('san leandro, ca 94577')