-
Notifications
You must be signed in to change notification settings - Fork 148
/
googlemaps.js
179 lines (153 loc) · 5.53 KB
/
googlemaps.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
169
170
171
172
173
174
175
176
177
178
179
var qs = require('querystring');
// http://code.google.com/apis/maps/documentation/places/
exports.places = function(latlng, radius, key, callback, sensor, types, lang, name) {
var args = {
location: latlng,
radius: radius,
key: key
}
if (types) args.types = types;
if (lang) args.lang = lang;
if (name) args.name = name;
args.sensor = sensor || 'false';
var path = '/maps/api/place/search/json?' + qs.stringify(args);
makeRequest(path, true, returnObjectFromJSON(callback));
}
exports.placeDetails = function(referenceId, key, callback, sensor, lang) {
var args = {
reference: referenceId,
key: key
}
if (lang) args.lang = lang;
args.sensor = sensor || 'false';
var path = '/maps/api/place/details/json?' + qs.stringify(args);
makeRequest(path, true, returnObjectFromJSON(callback));
}
// http://code.google.com/apis/maps/documentation/geocoding/
exports.geocode = function(address , callback , sensor , bounds , region , language){
var args = {
'address': address
}
if(bounds){ args.bounds = bounds; }
if(region){ args.region = region; }
if(language){ args.language = language; }
args.sensor = sensor || 'false';
var path = '/maps/api/geocode/json?' + qs.stringify(args);
makeRequest(path, false, returnObjectFromJSON(callback));
}
// http://code.google.com/apis/maps/documentation/geocoding/#ReverseGeocoding
exports.reverseGeocode = function(latlng , callback , sensor , language ){
var args = {
'latlng': latlng
}
if(language){ args.language = language; }
args.sensor = sensor || 'false';
var path = '/maps/api/geocode/json?' + qs.stringify(args);
makeRequest(path, false, returnObjectFromJSON(callback));
}
// http://code.google.com/apis/maps/documentation/distancematrix/
exports.distance = function(origins, destinations, callback, sensor, mode, alternatives, avoid, units, language){
var args = {
'origins': origins,
'destinations': destinations
}
args.sensor = sensor || 'false';
if(mode){ args.mode = mode; }
if(avoid){ args.avoid = avoid; }
if(units){ args.units = units; }
if(language){ args.language = language; }
var path = '/maps/api/distancematrix/json?' + qs.stringify(args);
makeRequest(path, false, returnObjectFromJSON(callback));
}
// http://code.google.com/apis/maps/documentation/directions/
exports.directions = function(origin , destination , callback , sensor , mode , waypoints , alternatives , avoid , units , language){
var args = {
'origin': origin,
'destination': destination
}
args.sensor = sensor || 'false';
if(mode){ args.mode = mode; }
if(waypoints){ args.waypoints = waypoints; }
if(alternatives){ args.alternatives = alternatives; }
if(avoid){ args.avoid = avoid; }
if(units){ args.units = units; }
if(language){ args.language = language; }
var path = '/maps/api/directions/json?' + qs.stringify(args);
makeRequest(path, false, returnObjectFromJSON(callback));
}
// http://code.google.com/apis/maps/documentation/elevation/
// http://code.google.com/apis/maps/documentation/elevation/#Locations
exports.elevationFromLocations = function(locations , callback , sensor){
var args = {
'locations': locations
}
args.sensor = sensor || 'false';
var path = '/maps/api/elevation/json?' + qs.stringify(args);
makeRequest(path, false, returnObjectFromJSON(callback));
}
// http://code.google.com/apis/maps/documentation/elevation/#Paths
exports.elevationFromPath = function(path , samples , callback , sensor){
var args = {
'path': path,
'samples': samples
}
args.sensor = sensor || 'false';
var reqPath = '/maps/api/elevation/json?' + qs.stringify(args);
makeRequest(reqPath, false, returnObjectFromJSON(callback));
}
// Helper function to check and convert an array of points, be it strings/numbers/etc
// into the format used by Google Maps for representing lists of latitude/longitude pairs
exports.checkAndConvertArrayOfPoints = function(input){
switch(typeof(input)){
case 'object':
if(input instanceof Array){
var output = [];
for(var i = 0; i < input.length; i++){
output.push(exports.checkAndConvertPoint(input[i]));
}
return output.join('|');
}
break;
case 'string':
return input;
}
throw(new Error("Unrecognized input: checkAndConvertArrayOfPoints accepts Arrays and Strings"));
}
// Helper function to check and convert an points, be it strings/arrays of numbers/etc
// into the format used by Google Maps for representing latitude/longitude pairs
exports.checkAndConvertPoint = function(input){
switch(typeof(input)){
case 'object':
if(input instanceof Array){
return input[0].toString() + ',' + input[1].toString();
}
break;
case 'string':
return input;
}
throw(new Error("Unrecognized input: checkAndConvertPoint accepts Arrays of Numbers and Strings"));
}
// Wraps the callback function to convert the output to a javascript object
var returnObjectFromJSON = function(callback){
return function(err, jsonString){
callback(err , JSON.parse(jsonString));
}
}
// Makes the request to Google Maps API.
// If secure is true, uses https. Otherwise http is used.
var makeRequest = function(path, secure, callback){
var protocol = (secure) ? require('https') : require('http')
, options = { host: 'maps.googleapis.com', path: path, }
protocol.get(options, function (res) {
var data = "";
var httpEnd = function(){
if (res.statusCode == 200) callback(null, data);
else callback(new Error("Response status code: " + res.statusCode), data);
};
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', httpEnd);
res.on('close', httpEnd);
}).on('error', function (err) { callback(err); });
}