-
Notifications
You must be signed in to change notification settings - Fork 0
/
getDestination.js
44 lines (32 loc) · 1.05 KB
/
getDestination.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
const https = require('https');
function urlGen(str) {
const query = strParser(str.trim());
return "/maps/api/geocode/json?address=" + query + "&key=AIzaSyC48wuJycPskG7D0PG0Ssq4TPGXMJC3pbM";
}
function strParser(str) {
return str.replace(/\s+/g, "_").replace(/\W/g, '');
}
module.exports = function(str) {
const path = urlGen(str);
const options = {
hostname: "maps.googleapis.com",
port: "443",
path: path,
method: 'GET'
}
return new Promise(function (resolve, reject) {
/*const req = */https.request(options, (res) => {
let chunks = new Buffer([])
res.on('data', (d) => {
chunks = Buffer.concat([chunks, d]);
});
res.on('end', () => {
var data = JSON.parse(chunks.toString("utf8"));
resolve(data.results[0].geometry.location);
})
res.on('error', (err) => {
reject(err);
});
}).end();
})
}