-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
126 lines (117 loc) · 3.23 KB
/
main.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
// initialize the map
let map = L.map("map").setView([30.28, -97.74], 11);
// load a tile layer
L.tileLayer("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
maxZoom: 18,
attribution:
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
}).addTo(map);
// Icon options
let iconOptions = {
iconUrl: "bus.svg",
iconSize: [15, 30],
};
// Creating a custom icon
let customIcon = L.icon(iconOptions);
// Creating Marker Options
let markerOptions = {
// title: "MyLocation",
// clickable: true,
// draggable: true,
icon: customIcon,
};
map.zoomControl.setPosition("bottomright");
let busMarkerLayer = L.layerGroup().addTo(map);
let sidebar = L.control.sidebar("sidebar").addTo(map);
drawRoutes();
getBusData();
document.getElementById("bus-refresh-button").innerHTML += "<p>Success</p>";
let interval = setInterval(getBusData, 15000);
// console.log("Initi"+interval)
function handleVisibilityChange() {
if (document.visibilityState === "hidden") {
console.log("Tab hidden");
console.log(interval);
clearInterval(interval);
} else {
console.log("Tab visible");
getBusData();
clearInterval(interval);
interval = setInterval(getBusData, 15000);
console.log(interval);
}
}
document.addEventListener("visibilitychange", handleVisibilityChange, false);
async function getBusData() {
console.log("Run");
const BUS_POSITION_URL = "https://data.texas.gov/api/views/cuc7-ywmd/";
fetch(BUS_POSITION_URL)
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then((data) => {
let newUrl =
BUS_POSITION_URL +
"files/" +
data.blobId +
"?filename=vehiclepositions.json";
fetch(newUrl)
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then((data) => {
drawBuses(data);
});
})
.catch((e) => {
console.warn(e.message);
});
}
async function drawBuses(viewdata) {
let bus_array = viewdata.entity;
busMarkerLayer.clearLayers();
bus_array.forEach((element) => {
let bus_number = element.id;
let position = element.vehicle.position;
let lat = position.latitude;
let lon = position.longitude;
let speed = position.speed;
let direction = position.bearing;
let marker = L.marker([lat, lon], markerOptions, {
rotationAngle: direction,
});
marker.setRotationAngle(direction);
marker.setRotationOrigin("center center");
let message = `
<b>Bus #${bus_number} </b> <br>
Speed ${speed} <br>
Direction ${direction} <br>
`;
marker.bindPopup(message).openPopup();
marker.addTo(busMarkerLayer);
});
}
async function refreshBuses() {
getBusData();
}
async function drawRoutes() {
fetch("Routes.geojson")
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then((data) => {
L.geoJSON(data).addTo(map);
})
.catch((e) => {
console.warn(e.message);
});
}