index.js
11.2 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
* Modules from the community: package.json
*/
var defaultRequest = require('request');
var waitress = require('waitress');
var check = require('check-types');
/**
* Internal modules
*/
var _makeRequest = require('./utils/makeRequest');
var _assignParams = require('./utils/assignParams');
var _jsonParser = require('./utils/jsonParser');
var _encodePolyline = require('./utils/encodePolylines');
var _getDefaultConfig = require('./config/getDefault');
var _constants = require('./config/constants');
var ACCEPTED_CONFIG_KEYS = _constants.ACCEPTED_CONFIG_KEYS;
var GOOGLEMAPS_ENDPOINTS = _constants.GOOGLEMAPS_ENDPOINTS;
var MAX_REQUEST_LENGTHS = _constants.MAX_REQUEST_LENGTHS;
var api = {
placeSearchText: require('./placeSearchText'),
placeSearch: require('./placeSearchNearby'),
placeDetails: require('./placeDetails'),
placeAutocomplete: require('./placeAutocomplete'),
geocode: require('./geocode'),
reverseGeocode: require('./reverseGeocode'),
distance: require('./distance'),
directions: require('./directions'),
elevationFromLocations: require('./elevationFromLocations'),
// TODO
// move this into an internal module + fix integration test
// elevationFromPath: require('./elevationFromPath'),
staticMap: require('./staticMap'),
streetView: require('./streetView'),
timezone: require('./timezone')
}
/**
* Constructor
*/
var GoogleMapsAPI = function(config, request) {
var pk = null;
if (!check.object(config)) {
config = {};
}
var clonedConfig = JSON.parse(JSON.stringify(config));
if (clonedConfig['google_private_key'] != null) {
pk = clonedConfig['google_private_key'];
delete clonedConfig['google_private_key'];
}
/**
* Calling _getDefaultConfig() inside the constructor ensure the config is not a singleton
*/
this.config = _assignParams(_getDefaultConfig(), clonedConfig, ACCEPTED_CONFIG_KEYS);
if (pk != null) {
this.config['google_private_key'] = pk;
}
if (typeof request !== 'function') {
request = defaultRequest;
}
this.request = request;
};
/**
* Endpoint: /maps/api/place/nearbysearch/json
* Google documentation reference: https://developers.google.com/places/documentation/search
*
* TODO: Maps API for Work customers should not include a client or signature parameter with their requests.
* TODO: params zagatselected
*/
GoogleMapsAPI.prototype.placeSearch = api.placeSearch
/**
* Endpoint: /maps/api/place/textsearch/json
* Google documentation reference: https://developers.google.com/places/documentation/search
*
* TODO: Maps API for Work customers should not include a client or signature parameter with their requests.
* TODO: params zagatselected
*/
GoogleMapsAPI.prototype.placeSearchText = api.placeSearchText
/**
*
* Endpoint: '/maps/api/place/details/json'
* Google documentation reference: https://developers.google.com/places/documentation/details
*
* Note: reference is deprecated. The reference advises to use placeId instead
* Note: sensor is no longer required
*/
GoogleMapsAPI.prototype.placeDetails = api.placeDetails
/**
*
* Endpoint: '/maps/api/place/autocomplete/json'
* Google documentation reference: https://developers.google.com/places/web-service/autocomplete
*/
GoogleMapsAPI.prototype.placeAutocomplete = api.placeAutocomplete
/**
* Server side geocoding. For client usage please refer to this URL:
* https://developers.google.com/maps/articles/geocodestrat#client
*
* Endpoint: '/maps/api/geocode/json'
* Google documentation reference: https://developers.google.com/maps/documentation/geocoding
*
* Use limitations: This service is generally designed for geocoding static (known in advance) addresses for placement of application content on a map; this service is not designed to respond in real time to user input, for example. For dynamic geocoding (for example, within a user interface element)
*
* Caching: Geocoding is a time and resource intensive task. Whenever possible, pre-geocode known addresses (using the Geocoding API described here or another geocoding service), and store your results in a temporary cache of your own design.
* Quotas: Users of the free API: 2,500 requests per 24 hour period. 5 requests per second.
Google Maps API for Work customers: 100,000 requests per 24 hour period. 10 requests per second.
*/
GoogleMapsAPI.prototype.geocode = api.geocode
/**
*
* Endpoint: '/maps/api/geocode/json'
* Google documentation reference: https://developers.google.com/maps/documentation/geocoding/#ReverseGeocoding
*/
GoogleMapsAPI.prototype.reverseGeocode = api.reverseGeocode
/**
*
* Endpoint: '/maps/api/distancematrix/json'
* Google documentation reference: https://developers.google.com/maps/documentation/distancematrix/
*
* Use limitations: Distance Matrix API URLs are restricted to approximately 2000 characters, after URL Encoding. As some Distance Matrix API service URLs may involve many locations, be aware of this limit when constructing your URLs. Note that different browsers, proxies, and servers may have different URL character limits as well.
*
* Quotas: Users of the free API: 100 elements per query. 100 elements per 10 seconds. 2 500 elements per 24 hour period.
Google Maps API for Work customers: 625 elements per query. 1 000 elements per 10 seconds. 100 000 elements per 24 hour period.
*/
GoogleMapsAPI.prototype.distance = api.distance
/**
* departureTime and arrivalTime must be passed as UNIX timestamp => Math.floor((new Date()).getTime()/1000)
*
* Endpoint: '/maps/api/directions/json'
* Google documentation reference: https://developers.google.com/maps/documentation/directions/
* note: This service is generally designed for calculating directions for static (known in advance) addresses for placement of application content on a map; this service is not designed to respond in real time to user input, for example. For dynamic directions calculations (for example, within a user interface element), consult the documentation for the JavaScript API V3 Directions Service.
*
* Quotas: Users of the free API: 2,500 directions requests per 24 hour period. Up to 8 waypoints allowed in each request. Waypoints are not available for transit directions. 2 requests per second.
Google Maps API for Work customers: 100,000 directions requests per 24 hour period. 23 waypoints allowed in each request. Waypoints are not available for transit directions. 10 requests per second.
*/
GoogleMapsAPI.prototype.directions = api.directions
/**
*
* Endpoint: '/maps/api/elevation/json'
* Google documentation reference: http://code.google.com/apis/maps/documentation/elevation/
*
* Quotas: Users of the free API: 2,500 directions requests per 24 hour period. 512 locations per request. 5 requests per second.
Google Maps API for Work customers: 100,000 directions requests per 24 hour period. 512 locations per request. 10 requests per second.
*/
GoogleMapsAPI.prototype.elevationFromLocations = api.elevationFromLocations
/**
*
* Endpoint: '/maps/api/elevation/json'
* Google documentation reference: http://code.google.com/apis/maps/documentation/elevation/#Paths
*/
GoogleMapsAPI.prototype.elevationFromPath = function(params, callback) {
return _elevationFromPath(this.request, this.config, params.path, params.samples, callback);
};
/**
* V2
* Endpoint: '/maps/api/staticmap'
* Google documentation reference: https://developers.google.com/maps/documentation/staticmaps/
*/
GoogleMapsAPI.prototype.staticMap = api.staticMap
/**
*
* Endpoint: '/maps/api/streetview'
* Google documentation reference: http://code.google.com/apis/maps/documentation/streetview
*/
GoogleMapsAPI.prototype.streetView = api.streetView
/**
*
* Endpoint: '/maps/api/timezone'
* Google documentation reference: http://code.google.com/apis/maps/documentation/timezone
*/
GoogleMapsAPI.prototype.timezone = api.timezone
/**
* TODO this doesn't belong here
* 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.
*
* This is call recursively
*/
GoogleMapsAPI.prototype.checkAndConvertArrayOfPoints = checkAndConvertPoint = function(input) {
if ('string' === typeof input) {
return input;
}
if (Array.isArray(input)) {
var output = [];
for (var i = 0; i < input.length; i++) {
output.push(checkAndConvertPoint(input[i]));
}
return output.join('|');
}
throw new Error('Unrecognized input: checkAndConvertArrayOfPoints accepts Arrays and Strings');
};
/**
* TODO this doesn't belong here
* 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
*/
GoogleMapsAPI.prototype.checkAndConvertPoint = function(input) {
if ('string' === typeof input) {
return input;
}
if (Array.isArray(input)) {
return input[0].toString() + ',' + input[1].toString();
}
throw new Error('Unrecognized input: checkAndConvertPoint accepts Arrays of Numbers and Strings');
};
module.exports = GoogleMapsAPI;
// TODO improve this and move to a separate file
var _elevationFromPath = function(request, config, path, samples, callback, sensor) {
var MAX_PATH_LENGTH = 1500;
if (config.encode_polylines === true) {
path = 'enc:' + _encodePolyline(path);
}
var args = {
'path': path,
'samples': samples,
'sensor': sensor || 'false'
};
var count = (path.length < MAX_PATH_LENGTH ? 1 : Math.ceil(path.length/MAX_PATH_LENGTH));
if (count === 1) {
_makeRequest(request, config, GOOGLEMAPS_ENDPOINTS['elevation'], args, _jsonParser(callback), MAX_REQUEST_LENGTHS['elevation']);
} else {
var done = waitress(count, function(err, results) {
if (err) {
if (typeof callback === 'function') {
return callback(err);
} else {
throw err;
}
}
results = results
.sort(function(a, b) { return a.n - b.n; })
.map(function(v) { return v.results; });
var status = 'OK';
var aggregated = [];
results.forEach(function(result) {
aggregated = aggregated.concat(result.results);
if (result.status !== 'OK') {
status = result.status;
}
});
results = {
results: aggregated,
status: status
};
return callback(null, results);
});
path = path.split('|');
var pieceSize = Math.ceil(path.length / count);
var n = 0;
while (path.length) {
var smallerPath = path.splice(0, pieceSize);
// google will throttle us if we launch all the requests together, so we have to stagger them.
(function(n, path) {
var samples = path.length;
path = path.join('|');
var cb = function(err, results) {
if (err) {
return done(err);
}
return done(null, { n: n, results: results });
};
setTimeout(function() {
_elevationFromPath(request, config, path, samples, cb, sensor);
}, Math.floor(Math.random() * config.stagger_time));
})(++n, smallerPath);
}
}
};