This repository was archived by the owner on Nov 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApi.js
More file actions
113 lines (99 loc) · 2.61 KB
/
Api.js
File metadata and controls
113 lines (99 loc) · 2.61 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
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
import env from './env.js'
import Util from './Util.js'
const util = new Util();
export default class Api {
computeFastestTrip(baseURL, selectedMode, selectedPlaces, currentPosition) {
let promises = [];
let result = [];
selectedPlaces.map((place, i) => {
promises.push(
this.computeTrip(baseURL, selectedMode, currentPosition, place)
.then((routingJSON => {
if (routingJSON.hasOwnProperty('error')) {
util.log(routingJSON);
return
}
if (!routingJSON.hasOwnProperty('groups')) {
return
}
representativeTrip = util.getBetterScore(routingJSON);
result.push({
'routingJSON': routingJSON,
'updateURL': representativeTrip.updateURL,
'arrive': representativeTrip.arrive,
'depart': representativeTrip.depart,
'carbon': representativeTrip.carbon,
'money': representativeTrip.money,
'currencySymbol': representativeTrip.currencySymbol,
'place': place
})
}))
);
});
return Promise.all(promises).then(() => {
return result.sort((a, b) => {
return a.arrive-b.arrive;
});
});
}
computeTrip(baseUrl, selectedMode, fromLoc, toLoc) {
data = {
fromLoc: `(${fromLoc.latitude},${fromLoc.longitude})`,
toLoc: `(${toLoc.latitude},${toLoc.longitude})`,
mode: selectedMode,
wp: '(1,1,1,1)',
v: 11,
includeStops: true
}
let url = baseUrl + '/routing.json'+
`?from=${data.fromLoc}&to=${data.toLoc}&modes=${data.mode}&wp=${data.wp}&v=${data.v}&includeStops=${data.includeStops}`
return fetch(url, {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-TripGo-Key': env.TRIPGO_API_KEY
}
})
.then((response) => response.json())
}
getModes(regionsJSON, regionName) {
let modes = regionsJSON.modes;
var result = new Array();
regionsJSON.regions.map(region => {
if (region.name.localeCompare(regionName) != 0)
return;
region.modes.map(mode => {
modes[mode].mode = mode;
result.push(modes[mode]);
});
})
return result;
}
getURLs(regionsJSON, regionName) {
let modes = regionsJSON.modes;
var result = null;
regionsJSON.regions.map(region => {
if (region.name.localeCompare(regionName) != 0)
return;
result = region.urls;
})
return result;
}
updateTrip(updateUrl) {
let url = updateUrl + '&v=11';
return fetch(url, {
method: 'GET',
headers: {
'Accept': 'application/json',
'X-TripGo-Key': env.TRIPGO_API_KEY
}
})
.then((response) => {
return response.json()
.catch(err => {
return {};
});;
})
}
}