-
Notifications
You must be signed in to change notification settings - Fork 1
/
directions.js
37 lines (31 loc) · 1.33 KB
/
directions.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
const axios = require('axios');
// const key = 'AIzaSyBjowTROtfYG8J7Ulwk4OUUemsGysQeWk4'
const key = 'AIzaSyBHcjlduXV-5AthZmZRZkmP2zv-dmtLyNM'
module.exports = (input) => {
input = input.toLowerCase();
if (input.indexOf("to ") > input.indexOf("from ")) {
destinationLocation = input.substring(input.indexOf("to ")+3);
departureLocation = input.substring(input.indexOf("from ")+5, input.indexOf("to ")-1);
//console.log("Destination: " + destinationLocation + "\nDeparture: " + departureLocation);
} else {
departureLocation = input.substring(input.indexOf("from ")+5);
destinationLocation = input.substring(input.indexOf("to ")+3, input.indexOf("from ")-1);
//console.log("Destination: " + destinationLocation + "\nDeparture: " + departureLocation);
}
return getDirections(departureLocation, destinationLocation)
}
function removeBrackets(input) {
return input
.replace(/{.*?}/g, "")
.replace(/\[.*?\]/g, "")
.replace(/<.*?>/g, "")
.replace(/\(.*?\)/g, "");
}
const getDirections = (origin, destination) => axios.get('https://maps.googleapis.com/maps/api/directions/json', {
params: {
origin,
destination,
key
}
})
.then(result => result.data.routes[0].legs[0].steps.map((e) => removeBrackets(e.html_instructions)).join('\n'))