-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapStations.py
102 lines (77 loc) · 3.46 KB
/
MapStations.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
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
from gmaps import getCitiesInCounty, getCounty
from googlemaps import Client as GoogleMaps
import requests
import json
Geocoder = GoogleMaps(key='AIzaSyDdxzG0lDmmZPJGxeOybGNkEtIL10YMxQY')
# enter your api key here
api_key = 'AIzaSyDdxzG0lDmmZPJGxeOybGNkEtIL10YMxQY'
# countyName = getCounty(94555) # should come from user input
# cities = ['Alameda', 'Oakland', 'Hayward', 'Pleasanton', 'Livermore', 'San Leandro', 'Berkeley', 'Dublin',
# 'Castro Valley', 'Union City', 'Newark', 'Emeryville', 'Albany', 'San Lorenzo', 'Piedmont', 'Sunol',
# 'Piedmont', 'Sunol', 'Cherryland', 'Fairview', 'Ashland']
# stateName = 'CA' # should come from user input
nearbyStation = []
stationAddresses = []
def stationCalc(address):
global nearbyStation
# url variable store url
url = "https://maps.googleapis.com/maps/api/place/textsearch/json?"
# The text string on which to search
query = 'Fire%20station'
location = Geocoder.geocode(address)
latitude = location[0]['geometry']['location']['lat']
longitude = location[0]['geometry']['location']['lng']
# get method of requests module
# return response object
r = requests.get(url + 'query=' + query + '&location=' + str(latitude) + ', ' + str(longitude) +
'&type=fire_station' + '&rankby=distance' + '&key=' + api_key)
# json method of response object convert
# json format data into python format data
x = r.json()
print(x)
# now x contains list of nested dictionaries
# we know dictionary contain key value pair
# store the value of result key in variable y
y = x['results']
# keep looping upto length of y
for i in range(min(len(y), 5)):
nearbyStation.append((y[i]['name']))
stationAddresses.append(y[i]['formatted_address'])
stationNumber = len(stationAddresses)
return stationNumber, stationAddresses, nearbyStation
nearbyHospital = []
hospitalAddresses = []
def hospitalCalc(address):
global nearbyHospital
global hospitalAddresses
# url variable store url
url = "https://maps.googleapis.com/maps/api/place/textsearch/json?"
# The text string on which to search
query = 'Hospital'
location = Geocoder.geocode(address)
latitude = location[0]['geometry']['location']['lat']
longitude = location[0]['geometry']['location']['lng']
# get method of requests module
# return response object
r = requests.get(url + 'query=' + query + '&location=' + str(latitude) + ', ' + str(longitude) +
'&type=hospital' + '&rankby=distance' + '&key=' + api_key)
# json method of response object convert
# json format data into python format data
x = r.json()
print(x)
# now x contains list of nested dictionaries
# we know dictionary contain key value pair
# store the value of result key in variable y
y = x['results']
# keep looping upto length of y
for i in range(min(len(y),5)):
hospitalName = y[i]['name']
nearbyHospital.append(hospitalName)
hospitalAddresses.append(y[i]['formatted_address'])
hospitalNumber = len(nearbyHospital)
return hospitalNumber, hospitalAddresses, nearbyHospital
if __name__ == "__main__":
AlamedaStationNum, AlamedaStationAddresses, AlamedaStationNames = stationCalc('3749 Armour Ct, Fremont, CA')
AlamedaHospitalNum, AlamedaHospAddresses, AlamedaHospNames = hospitalCalc('3749 Armour Ct, Fremont, CA')
print(AlamedaStationAddresses)
print(AlamedaHospAddresses)