-
Notifications
You must be signed in to change notification settings - Fork 714
/
MashItUp.jsc
67 lines (56 loc) · 2.36 KB
/
MashItUp.jsc
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
// Initialize the response that might already be hanging around
response.content = '';
response.headers['Content-Type'] = 'application/json';
try {
if ((request.queryParams.postalcode == undefined) ||
(request.queryParams.country == undefined)) {
throw '"postalcode" and "country" query parameters are required';
}
// Send an HTTP GET to the URL that we construct
var geocoding = httpClient.get(
'http://maps.googleapis.com/maps/api/geocode/json?address=' +
request.queryParams.postalcode +
'®ion=' + request.queryParams.country +
'&sensor=false');
geocoding.waitForComplete();
if (!geocoding.isSuccess()) {
throw 'Error contacting geocoding web service';
}
// We got a response. Parse the JSON into a JavaScript object.
geocodeResponse = geocoding.getResponse().content.asJSON;
if (geocodeResponse.status != 'OK') {
throw 'Error returned from geocoding web service: ' + geocodeResponse.status;
}
// Go through the JavaScript returned by Google and get the results
var lat = geocodeResponse.results[0].geometry.location.lat;
var lng = geocodeResponse.results[0].geometry.location.lng;
// Send another HTTP GET to the other service
var altitude = httpClient.get(
'http://maps.googleapis.com/maps/api/elevation/json?sensor=false&locations=' +
lat + ',' + lng);
altitude.waitForComplete();
if (!altitude.isSuccess()) {
throw 'Error contacting altitude web service';
}
altitudeResponse = altitude.getResponse().content.asJSON;
if (altitudeResponse.status != 'OK') {
throw 'Error returned from altitude web service: ' + altitudeResponse.status;
}
var alt = altitudeResponse.results[0].elevation;
// Assemble the parts of the response as a JavaScript object
var location = new Object();
location.latitude = lat;
location.longitude = lng;
var altitude = new Object();
altitude.meters = alt;
altitude.feet = alt * 3.2808399;
// Final assembly, then turn it into a JSON object
var body = response.content.asJSON;
body.country = request.queryParams.country;
body.postalcode = request.queryParams.postalcode;
body.elevation = location;
body.altitude = altitude;
} catch (err) {
// Handle any error that may have happened previously by generating a response
response.content.asJSON.error = err;
}