-
Notifications
You must be signed in to change notification settings - Fork 6
/
maps.js
338 lines (315 loc) · 11.5 KB
/
maps.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
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
331
332
333
334
335
336
337
338
/**
* bing - maps
* Copyright (C) 2011 Justin Walgran
* MIT Licensed
*/
/**
* This module uses the request module as a high level API for consuming the
* Bing REST services.
* @private
*/
var request = require('request'),
/**
* The base URL for Bing services is cached in a variable so it is not
* repeated thoughout the module code.
* @type {String}
* @private
*/
SERVICE_URL = 'http://dev.virtualearth.net/REST/v1',
/**
* This URL fragment is concatenated with the `SERVICE_URL` to build
* the full URL used to make requests for transit routes.
* @type {String}
* @private
*/
TRANSIT_RESOURCE = 'Routes/Transit',
/**
* This URL fragment is concatenated with the `SERVICE_URL` to build
* the full URL used to make requests for walking routes.
* @type {String}
* @private
*/
WALKING_RESOURCE = 'Routes/Walking',
/**
* This URL fragment is concatenated with the `SERVICE_URL` to build
* the full URL used to make requests for walking routes.
* @type {String}
* @private
*/
DRIVING_RESOURCE = 'Routes/Driving',
/**
* Each application needs a distinct API key so this module expects a
* `BING_MAPS_API_KEY` environment variable to be defined.
* @type {String}
* @private
*/
BING_MAPS_API_KEY = process.env.BING_MAPS_API_KEY,
/**
* Treat the specified `obj` object as a flat collection of key-value pairs
* and convert it to a query string prefixed with ? and ready to be concatenated
* with a URL.
* @param {Object} obj The object to be converted into a query string.
* @private
* returns String
*/
objectToQueryString = function(obj) {
if (obj) {
var args = [];
for(var key in obj)
args.push(key + '=' + encodeURIComponent(obj[key]));
return '?' + args.join('&');
}
else {
return '';
}
},
/**
* Build a URL by concatenating the specified `queryArgs` string with
* a base URL to create a full URL.
* @param {String} queryArgs A properly formatted query argument string
* prefixed with '?'.
* @private
* returns String
*/
createTransitUrl = function(queryArgs) {
return SERVICE_URL + '/' + TRANSIT_RESOURCE + objectToQueryString(queryArgs);
},
/**
* Build a URL by concatenating the specified `queryArgs` string with
* a base URL to create a full URL to the walking route service.
* @param {String} queryArgs A properly formatted query argument string
* prefixed with '?'.
* @private
* returns String
*/
createWalkingUrl = function(queryArgs) {
return SERVICE_URL + '/' + WALKING_RESOURCE + objectToQueryString(queryArgs);
},
/**
* Build a URL by concatenating the specified `queryArgs` string with
* a base URL to create a full URL to the walking route service.
* @param {String} queryArgs A properly formatted query argument string
* prefixed with '?'.
* @private
* returns String
*/
createDrivingUrl = function(queryArgs) {
return SERVICE_URL + '/' + DRIVING_RESOURCE + objectToQueryString(queryArgs);
},
/**
* Create a default hash of options for Bing Maps web service calls.
* - optimize = time
* - distanceUnit = mi (miles)
* - outputType = json
* @private
* returns Object
*/
getDefaultOptions = function() {
curTime = new Date();
return {
'optimize': 'time', // The Bing API can also accept 'distance' or 'timeWithTraffic'
'distanceUnit': 'mi', // The Bing API can also accept 'km'
'outputType': 'json' // The Bing API can also accept 'xml'
};
},
/**
* Create a default options hash specific to transit directions web service requests.
* - optimize = time
* - distanceUnit = mi (miles)
* - travelTime = current time in UTC
* - timeType = Departure
* - maxSolutionCount = 3
* - outputType = json
* @private
* returns Object
*/
getDefaultTransitOptions = function() {
var options = getDefaultOptions();
options['travelTime'] = (curTime.getUTCMonth()+1) + "/" + curTime.getUTCDate() + "/" + curTime.getUTCFullYear() + " " + curTime.getUTCHours() + ":" + curTime.getUTCMinutes() + ":" + curTime.getUTCSeconds();
options['timeType'] = 'Departure'; // The Bing API can also accept 'Arrival' or 'LastAvailable'
options['maxSolutionCount'] = 3;
return options;
},
/**
* Create a default options hash specific to walking directions web service requests.
* - optimize = distance
* - distanceUnit = mi (miles)
* - outputType = json
* @private
* returns Object
*/
getDefaultWalkingOptions = function() {
var options = getDefaultOptions();
options['optimize'] = 'distance';
return options
},
/**
* Create a default options hash specific to driving directions web service requests.
* - optimize = distance
* - distanceUnit = mi (miles)
* - outputType = json
* @private
* returns Object
*/
getDefaultDrivingOptions = function() {
var options = getDefaultOptions();
options['optimize'] = 'time';
return options
},
/**
* Create an options hash that can be converted into query string arguments for appending
* to a Bing Maps rest service URL. This function chandles converting more readable option
* names like 'outputType' to 'o' and converting the specified startLocation and endLocation
* to 'wp.0' and 'wp.1'.
* @param {String} startLocation The starting address for the directions.
* @param {String} endLocation The destination address for the directions.
* @param {Object} a hash of key value options to be converted.
* @private
* returns Object
*/
convertLocationsAndOptionsToQueryStringArgs = function(startLocation, endLocation, options) {
var args = {
'wp.0': startLocation,
'wp.1': endLocation,
'key': BING_MAPS_API_KEY,
'o': options.outputType,
'optmz': options.optimize,
'du': options.distanceUnit
};
if (options.travelTime) {
args['dt'] = options.travelTime;
}
if (options.timeType) {
args['tt'] = options.timeType;
}
if (options.maxSolutionCount) {
args['maxSolutions'] = options.maxSolutionCount;
}
return args;
},
/**
* Make a request to the Bing maps API.
* @param {String} url A Bing Maps REST API url with query string arguments.
* @param {Function} callback The function to call when a response is received
* from the Bing API or an error occurs.
* @private
*/
callBingApi = function(url, callback) {
request(url, function (err, response, body) {
if (!err && response.statusCode == 200) {
if (callback) {
callback(undefined, JSON.parse(body));
}
}
else {
if (callback) {
callback(err, {"error": {"statusCode": response.statusCode, "body": JSON.parse(body)}});
}
}
});
};
/**
* Make a request to the Bing maps API to get transit directions between
* two addresses.
* @public
* @param {String} startLocation The starting address for the directions.
* @param {String} endLocation The destination address for the directions.
* @param {Function} callback The function to call when a response is received
* from the Bing API or an error occurs.
*
* If the request to the Bing API returns an error, or a non 200 response code
* the the specified callback will be invoked with the an error object as the
* first parameter.
* @example {"error":{
* "statusCode": <The HTTP code returned from the Bing API request>,
* "body": <The raw text response returned from the Bing API>
* }}
*
* @example var callback = function(err, obj) {
* if(!err) {
* // Process the response object, which was created by parsing the JSON
* // returned from the Bing API.
* } else {
* // Handle the error.
* }
* };
* bing.maps.getTransitRoute('100 N Broad St, Philadelphia','908 N 3rd St., Philadelphia', callback);
*
* @since 0.0.1
*/
exports.getTransitRoute = function(startLocation, endLocation, callback) {
var options = getDefaultTransitOptions(),
queryStringArgs = convertLocationsAndOptionsToQueryStringArgs(startLocation, endLocation, options),
requestUrl = createTransitUrl(queryStringArgs);
callBingApi(requestUrl, callback);
}
/**
* Make a request to the Bing maps API to get walking directions between
* two addresses.
* @public
* @param {String} startLocation The starting address for the directions.
* @param {String} endLocation The destination address for the directions.
* @param {Function} callback The function to call when a response is received
* from the Bing API or an error occurs.
*
* If the request to the Bing API returns an error, or a non 200 response code
* the the specified callback will be invoked with the an error object as the
* first parameter.
* @example {"error":{
* "statusCode": <The HTTP code returned from the Bing API request>,
* "body": <The raw text response returned from the Bing API>
* }}
*
* @example var callback = function(err, obj) {
* if(!err) {
* // Process the response object, which was created by parsing the JSON
* // returned from the Bing API.
* } else {
* // Handle the error.
* }
* };
* bing.maps.getWalkingRoute('100 N Broad St, Philadelphia','908 N 3rd St., Philadelphia', callback);
*
* @since 0.0.3
*/
exports.getWalkingRoute = function(startLocation, endLocation, callback) {
var options = getDefaultWalkingOptions(),
queryStringArgs = convertLocationsAndOptionsToQueryStringArgs(startLocation, endLocation, options),
requestUrl = createWalkingUrl(queryStringArgs);
callBingApi(requestUrl, callback);
}
/**
* Make a request to the Bing maps API to get driving directions between
* two addresses.
* @public
* @param {String} startLocation The starting address for the directions.
* @param {String} endLocation The destination address for the directions.
* @param {Function} callback The function to call when a response is received
* from the Bing API or an error occurs.
*
* If the request to the Bing API returns an error, or a non 200 response code
* the the specified callback will be invoked with the an error object as the
* first parameter.
* @example {"error":{
* "statusCode": <The HTTP code returned from the Bing API request>,
* "body": <The raw text response returned from the Bing API>
* }}
*
* @example var callback = function(err, obj) {
* if(!err) {
* // Process the response object, which was created by parsing the JSON
* // returned from the Bing API.
* } else {
* // Handle the error.
* }
* };
* bing.maps.getDrivingRoute('100 N Broad St, Philadelphia','908 N 3rd St., Philadelphia', callback);
*
* @since 0.0.6
*/
exports.getDrivingRoute = function(startLocation, endLocation, callback) {
var options = getDefaultDrivingOptions(),
queryStringArgs = convertLocationsAndOptionsToQueryStringArgs(startLocation, endLocation, options),
requestUrl = createDrivingUrl(queryStringArgs);
callBingApi(requestUrl, callback);
}