forked from graphhopper/directions-api-js-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphHopperIsochrone.js
60 lines (49 loc) · 1.68 KB
/
GraphHopperIsochrone.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
var request = require('superagent');
var Promise = require("bluebird");
var GHUtil = require("./GHUtil");
var ghUtil = new GHUtil();
GraphHopperIsochrone = function (args) {
this.time_limit = 600;
this.distance_limit = 0;
this.buckets = 3;
this.vehicle = "car";
this.point;
this.host = "https://graphhopper.com/api/1";
this.debug = false;
this.basePath = '/isochrone';
this.timeout = 30000;
this.reverse_flow = false;
ghUtil.copyProperties(args, this);
};
GraphHopperIsochrone.prototype.getParametersAsQueryString = function (args) {
var qString = "point=" + args.point;
qString += "&time_limit=" + args.time_limit;
qString += "&distance_limit=" + args.distance_limit;
qString += "&buckets=" + args.buckets;
qString += "&vehicle=" + args.vehicle;
qString += "&reverse_flow=" + args.reverse_flow;
if (args.debug)
qString += "&debug=true";
return qString;
};
GraphHopperIsochrone.prototype.doRequest = function (reqArgs) {
var that = this;
return new Promise(function(resolve, reject) {
var args = ghUtil.clone(that);
if (reqArgs)
args = ghUtil.copyProperties(reqArgs, args);
var url = args.host + args.basePath + "?" + that.getParametersAsQueryString(args) + "&key=" + args.key;
request
.get(url)
.accept('application/json')
.timeout(args.timeout)
.end(function (err, res) {
if (err || !res.ok) {
reject(ghUtil.extractError(res, url));
} else if (res) {
resolve(res.body);
}
});
});
};
module.exports = GraphHopperIsochrone;