-
Notifications
You must be signed in to change notification settings - Fork 0
/
geocode.js
47 lines (44 loc) · 1.35 KB
/
geocode.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
/**
* Geocodes an address with the Google API
* @param {string} inputAddress The address to geocode
* @return The comma separated latitute and longitude
* @customfunction
*/
function GEOCODE(inputAddress) {
if (typeof inputAddress != "string" || inputAddress.length < 4) {
return "Address must be a string";
}
var lat, lon, err;
var cache = CacheService.getDocumentCache();
// Check cache first
var cachedResult = cache.get(inputAddress);
if (cachedResult !== null) {
var split = cachedResult.split(", ");
if (split[1] === undefined) {
return cachedResult;
}
lat = split[0];
lon = split[1];
} else {
// Otherwise new request
var params = "?address="+encodeURIComponent(inputAddress);
var res = UrlFetchApp.fetch("https://maps.googleapis.com/maps/api/geocode/json"+params);
var result = JSON.parse(res.getContentText());
if (result.status == "OK") {
lat = result.results[0].geometry.location.lat;
lon = result.results[0].geometry.location.lng;
// Cache Result
cache.put(inputAddress, (lat + ", " + lon));
} else if(result.status == "ZERO_RESULTS") {
err = "no results";
cache.put(inputAddress, err);
return err;
} else {
err = "ERROR";
cache.put(inputAddress, err);
return err;
}
}
// Return lat, lon
return [[lat, lon]];
}