-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
51 lines (45 loc) · 1.12 KB
/
index.js
File metadata and controls
51 lines (45 loc) · 1.12 KB
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
//jshint esversion:6
require('dotenv').config();
const API_KEY = process.env.GOOGLE_API_KEY;
const googleMapsClient = require('@google/maps').createClient({
key: API_KEY,
Promise: Promise
});
var getTime = (origin, destination) =>{
return new Promise( (resolve, reject ) => {
googleMapsClient.distanceMatrix({
origins: [
origin
],
destinations: [
destination
],
language: 'en',
units: 'metric',
'departure_time':'now',
'traffic_model': 'best_guess'
}).asPromise()
.then((response) => {
let data = {
status: response.json.status,
origin: response.json.origin_addresses[0],
destination: response.json.destination_addresses[0],
distance: response.json.rows[0].elements[0].distance.text,
duration: response.json.rows[0].elements[0].duration.text
};
resolve(data);
})
.catch((err) => {
reject(err);
});
});
};
getTime('Kuala Lumpur, Federal Territory of Kuala Lumpur, Malaysia', 'KL International Airport').then((result) =>{
// console.dir(result, {colors:true});
}).catch((err) =>{
// console.log(err);
});
module.exports = {
API_KEY,
getTime
};