-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgetGeo.js
169 lines (159 loc) · 4.66 KB
/
getGeo.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* Looks up an address' probable ZIP code using the
* Google geocoder.
*
* @param {string} address An address string (May consist of multiple cells, such as street, city, and state).
* @return The ZIP of first matched address.
* @customfunction
*/
function getZip(address) {
if (address == '') {
Logger.log("Must provide an address");
return;
}
var geocoder = Maps.newGeocoder();
var location;
Utilities.sleep(Math.random() * 4000);
location = geocoder.geocode(address);
if (location.status == 'OK') {
zip = extractFromAddress(location["results"][0].address_components, "postal_code");
return zip;
}
};
/**
* Looks up an address' probable County using the
* Google geocoder.
*
* @param {string} address An address string (May consist of multiple cells, such as street, city, and state).
* @return The County of first matched address.
* @customfunction
*/
function getCounty(address) {
if (address == '') {
Logger.log("Must provide an address");
return;
}
var geocoder = Maps.newGeocoder();
Utilities.sleep(Math.random() * 4000);
var location;
location = geocoder.geocode(address);
if (location.status == 'OK') {
county = extractFromAddress(location["results"][0].address_components, "administrative_area_level_2");
return county;
}
};
/**
* Looks up an address' probable City using the Google geocoder.
*
* @param {string} address An address string (May consist of multiple cells, such as street, state, and zip).
* @return The City of first matched address.
* @customfunction
*/
function getCity(address) {
if (address == '') {
Logger.log("Must provide an address");
return;
}
var geocoder = Maps.newGeocoder();
Utilities.sleep(Math.random() * 4000);
var location;
var city;
location = geocoder.geocode(address);
if (location.status == 'OK') {
city = extractFromAddress(location["results"][0].address_components, "locality");
return city;
}
};
function extractFromAddress(components, type){
for (var i=0; i<components.length; i++)
for (var j=0; j<components[i].types.length; j++)
if (components[i].types[j]==type) return components[i].long_name;
return "";
}
/**
* Looks up an address' probable Latitude using the Google geocoder.
*
* @param {string} address An address string (May consist of multiple cells, such as street, state, and zip).
* @return The Latitude of first matched address.
* @customfunction
*/
function getLat(address) {
if (address == '') {
Logger.log("Must provide an address");
return;
}
var geocoder = Maps.newGeocoder();
Utilities.sleep(Math.random() * 4000);
var location;
var latitude;
loc = geocoder.geocode(address);
if (loc.status == 'OK') {
latitude = loc["results"][0]['geometry']['location']['lat'];
return latitude;
}
};
/**
* Looks up an address' probable Latitude using the Google geocoder.
*
* @param {string} address An address string (May consist of multiple cells, such as street, state, and zip).
* @return The longitude of first matched address.
* @customfunction
*/
function getLng(address) {
if (address == '') {
Logger.log("Must provide an address");
return;
}
var geocoder = Maps.newGeocoder();
Utilities.sleep(Math.random() * 4000);
var location;
var longitude;
loc = geocoder.geocode(address);
if (loc.status == 'OK') {
longitude = loc["results"][0]['geometry']['location']['lng'];
return longitude;
}
};
/**
* How many results (for instance, zip codes, counties, cities, etc.) could match the given address
*
* @param {string} address An address string (May consist of multiple cells, such as street, city, and state).
* @return The count of resulting matches.
* @customfunction
*/
function getGeoCount(address) {
if (address == '') {
Logger.log("Must provide an address");
return;
}
var geocoder = Maps.newGeocoder();
var location;
Utilities.sleep(Math.random() * 4000);
location = geocoder.geocode(address);
if (location.status == 'OK') {
count = location["results"].length;
return count;
}
};
/**
* Looks up an address' probable Country using the Google Map API.
*
* @param {string} address An address string (May consist of multiple cells, such as street, state, and zip).
* @return The Country of first matched address.
* @customfunction
*/
function getCountry(address) {
if (address == '') {
Logger.log("Must provide an address");
return;
}
var geocoder = Maps.newGeocoder();
Utilities.sleep(Math.random() * 4000);
var location;
var city;
location = geocoder.geocode(address);
if (location.status == 'OK') {
city = extractFromAddress(location["results"][0].address_components, "country");
return city;
}
};