forked from graphhopper/directions-api-js-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphHopperMatrix.js
118 lines (97 loc) · 3.38 KB
/
GraphHopperMatrix.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
var request = require('superagent');
var Promise = require("bluebird");
var GHUtil = require("./GHUtil");
var ghUtil = new GHUtil();
GraphHopperMatrix = function (args) {
this.host = "https://graphhopper.com/api/1";
this.vehicle = "car";
this.debug = false;
this.from_points = [];
this.to_points = [];
this.out_arrays = [];
this.basePath = "/matrix";
this.graphhopper_maps_host = "https://graphhopper.com/maps/?";
this.timeout = 30000;
ghUtil.copyProperties(args, this);
};
GraphHopperMatrix.prototype.addPoint = function (latlon) {
this.addFromPoint(latlon);
this.addToPoint(latlon);
};
GraphHopperMatrix.prototype.addFromPoint = function (latlon) {
this.from_points.push(latlon);
};
GraphHopperMatrix.prototype.addToPoint = function (latlon) {
this.to_points.push(latlon);
};
GraphHopperMatrix.prototype.clearPoints = function () {
this.from_points.length = 0;
this.to_points.length = 0;
};
GraphHopperMatrix.prototype.addOutArray = function (type) {
this.out_arrays.push(type);
};
GraphHopperMatrix.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 + "?vehicle=" + args.vehicle + "&key=" + args.key;
for (var idx in args.from_points) {
var p = args.from_points[idx];
url += "&from_point=" + encodeURIComponent(p.toString());
}
for (var idx in args.to_points) {
var p = args.to_points[idx];
url += "&to_point=" + encodeURIComponent(p.toString());
}
if (args.out_arrays) {
for (var idx in args.out_arrays) {
var type = args.out_arrays[idx];
url += "&out_array=" + type;
}
}
if (args.debug)
url += "&debug=true";
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);
}
});
});
};
GraphHopperMatrix.prototype.toHtmlTable = function (doubleArray) {
var htmlOut = "<table border='1' cellpadding='10'>";
// header => to points
htmlOut += "<tr>";
// upper left corner:
htmlOut += "<td>↓ from \ to →</td>";
// header with to points
for (var idxTo in this.to_points) {
htmlOut += "<td><b>" + this.to_points[idxTo] + "</b></td>";
}
htmlOut += "</tr>";
for (var idxFrom in doubleArray) {
htmlOut += "<tr>";
htmlOut += "<td><b>" + this.from_points[idxFrom] + "</b></td>";
var res = doubleArray[idxFrom];
for (var idxTo in res) {
var mapsURL = this.graphhopper_maps_host
+ "point=" + encodeURIComponent(this.from_points[idxFrom])
+ "&point=" + encodeURIComponent(this.to_points[idxTo])
+ "&vehicle=" + this.vehicle;
htmlOut += "<td> <a href='" + mapsURL + "'>" + res[idxTo] + "</a> </td>";
}
htmlOut += "</tr>\n";
}
htmlOut += "</table>";
return htmlOut;
};
module.exports = GraphHopperMatrix;