forked from onexi/starter-realtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
04_mbta_data.html
47 lines (34 loc) · 890 Bytes
/
04_mbta_data.html
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
<!-- --------------------------------- -->
<!-- ------- Request MBTA data ------- -->
<!-- --------------------------------- -->
<script>
// Request bus data from MBTA
async function getBusLocations(){
// MBTA real-time data for bus route 1
const url = 'https://api-v3.mbta.com/vehicles?filter[route]=1&include=trip';
var response = await fetch(url);
var json = await response.json();
// console.log(json);
return json;
}
// request data on a timer
async function run(){
// request bus data
var buses = await getBusLocations();
// loop buses
buses.data.forEach(function(bus){
var id = bus.id;
var lat = bus.attributes.latitude;
var long = bus.attributes.longitude;
console.log(
'id:' + id +
',lat' + lat +
',long' + long
);
});
// timer
setTimeout(run, 15000);
}
run();
getBusLocations();
</script>