forked from graphhopper/directions-api-js-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphHopperGeocoding.js
73 lines (59 loc) · 2.05 KB
/
GraphHopperGeocoding.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
var request = require('superagent');
var Promise = require("bluebird");
var GHUtil = require("./GHUtil");
var ghUtil = new GHUtil();
GraphHopperGeocoding = function (args) {
// prefer results from a certain location (type: GHInput)
this.location_bias;
// the query
this.query;
this.host = "https://graphhopper.com/api/1";
this.debug = false;
this.locale = "en";
this.basePath = '/geocode';
this.timeout = 10000;
ghUtil.copyProperties(args, this);
};
GraphHopperGeocoding.prototype.clearLocation = function () {
this.location_bias = undefined;
};
GraphHopperGeocoding.prototype.getParametersAsQueryString = function (args) {
var qString = "locale=" + args.locale;
if (args.query) {
qString += "&q=" + encodeURIComponent(args.query);
if (args.location_bias)
qString += "&point=" + encodeURIComponent(args.location_bias.toString());
else if (args.point)
qString += "&point=" + encodeURIComponent(args.point.toString());
} else {
qString += "&reverse=true";
if (args.point)
qString += "&point=" + encodeURIComponent(args.point.toString());
}
if (args.debug)
qString += "&debug=true";
if (args.limit)
qString += "&limit=" + args.limit;
return qString;
};
GraphHopperGeocoding.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 = GraphHopperGeocoding;