forked from JayaSurya-27/GSM_Module
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmap_with_next_stop.html
More file actions
251 lines (235 loc) · 10.2 KB
/
map_with_next_stop.html
File metadata and controls
251 lines (235 loc) · 10.2 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Map with Last Updated Time and Pythagorean Distance</title>
<!-- Leaflet CSS and JS -->
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=" crossorigin=""/>
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" crossorigin=""></script>
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css2?family=Raleway:wght@400;700&family=Roboto:wght@400;500&display=swap" rel="stylesheet">
<style>
html, body {
height: 100%;
margin: 0;
font-family: 'Raleway', sans-serif;
background: #f0f0f0;
}
.container {
display: flex;
height: 100vh;
}
.map-container {
width: 480px;
height: 100%;
background: #fff;
border: 1px solid #ddd;
box-shadow: 2px 2px 5px rgba(0,0,0,0.1);
margin: 0;
}
#map {
width: 100%;
height: 100%;
}
.sidebar {
flex: 1;
padding: 20px;
background: linear-gradient(145deg, #fafafa, #f0f0f0);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.sidebar h2 {
margin-top: 0;
font-size: 24px;
color: #d42e2e;
}
.sidebar p {
font-size: 18px;
color: #555;
text-align: center;
margin: 10px 0;
}
.sidebar .updated-time {
font-size: 20px;
color: #3498db;
font-family: 'Roboto', sans-serif;
font-weight: 500;
letter-spacing: 0.5px;
}
.yes{
display: none;
}
/* .color{
font-size: 20px;
color: red;
font-family: 'Roboto', sans-serif;
font-weight: 500;
letter-spacing: 0.5px;
} */
</style>
</head>
<body>
<div class="container">
<div class="map-container">
<div id="map"></div>
</div>
<div class="sidebar">
<h2>Bus Tracking webpage</h2>
<p>Last Updated:</p>
<p class="updated-time" id="lastUpdate">Loading...</p>
<p>Current Speed:</p>
<p class="updated-time" id="Speed">Calculating...</p>
<p id="heading">Next Stop:</p>
<p class="updated-time" id="Nextstop">...</p>
</div>
</div>
<script>
// Initialize the Leaflet map
var map = L.map('map').setView([51.505, -0.09], 13);
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© OpenStreetMap'
}).addTo(map);
var marker;
// Initialization of the stops and time according to it
const stops = [
{ name: "Hostel PC", time: "08:00:00", latitude: 40.7128, longitude: -74.0060 },
{ name: "Main Gate", time: "08:05:00", latitude: 34.0522, longitude: -118.2437 },
{ name: "Bhoopali", time: "08:25:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Jubli Circle", time: "08:40:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "New Bust Stand Dharwad", time: "08:50:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Main Gate", time: "09:10:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Hostel PC", time: "09:20:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Hostel PC", time: "12:10:00", latitude: 40.7128, longitude: -74.0060 },
{ name: "A1-A2 Block", time: "12:15:00", latitude: 34.0522, longitude: -118.2437 },
{ name: "Main Gate", time: "12:20:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Bhoopali", time: "12:40:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Bhoopali", time: "12:45:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Main Gate", time: "13:00:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "A1-A2 Block", time: "13:05:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Hostel PC", time: "13:10:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Hostel PC", time: "14:50:00", latitude: 40.7128, longitude: -74.0060 },
{ name: "A1-A2 Block", time: "14:55:00", latitude: 34.0522, longitude: -118.2437 },
{ name: "Main Gate", time: "15:00:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Bhoopali", time: "15:20:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Bhoopali", time: "16:35:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Main Gate", time: "16:50:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "A1-A2 Block", time: "16:55:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Hostel PC", time: "17:00:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Hostel PC", time: "17:15:00", latitude: 40.7128, longitude: -74.0060 },
{ name: "A1-A2 Block", time: "17:20:00", latitude: 34.0522, longitude: -118.2437 },
{ name: "Main Gate", time: "17:25:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Bhoopali", time: "17:45:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Bhoopali", time: "17:45:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Main Gate", time: "18:00:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Hostel PC", time: "18:10:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Main Gate", time: "18:15:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Bhoopali", time: "18:35:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Bhoopali", time: "18:40:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "New Bus Stand Dharwad", time: "19:00:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "New Bus Stand Dharwad", time: "19:05:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Bhoopali", time: "19:25:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Bhoopali", time: "19:30:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Main Gate", time: "19:45:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Hostel PC", time: "20:00:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Hostel PC", time: "20:05:00", latitude: 40.7128, longitude: -74.0060 },
{ name: "Main Gate", time: "20:10:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Bhoopali", time: "20:30:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Bhoopali", time: "20:35:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Main Gate", time: "20:50:00", latitude: 51.5074, longitude: -0.1278 },
{ name: "Hostel PC", time: "21:05:00", latitude: 51.5074, longitude: -0.1278 },
];
// Function to convert UTC to IST string
function convertUTCtoIST(utcDateString) {
const utcDate = new Date(utcDateString);
const istOptions = {
timeZone: 'Asia/Kolkata',
hour12: false,
year: 'numeric',
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric'
};
return utcDate.toLocaleString('en-IN', istOptions);
}
// Function to convert HH:MM:SS to total seconds
function timeToSeconds(time) {
const [hours, minutes, seconds] = time.split(":").map(Number);
return hours * 3600 + minutes * 60 + seconds;
}
// Function to check if the current time is within the given interval
function isTimeInInterval(currentTime, startTime, endTime) {
const currentSeconds = timeToSeconds(currentTime);
const startSeconds = timeToSeconds(startTime);
const endSeconds = timeToSeconds(endTime);
return currentSeconds >= startSeconds && currentSeconds <= endSeconds;
}
// Function to update map data and determine next stop
function updateMapData() {
fetch('https://api.thingspeak.com/channels/2500568/feeds.json?api_key=3TVTGFNPIZH5T08Y&results=2')
.then(response => response.json())
.then(data => {
var latestFeed = data.feeds[data.feeds.length - 1];
var lat = parseFloat(latestFeed.field1);
var lon = parseFloat(latestFeed.field2);
var spd = parseInt(latestFeed.field3);
var lastUpdatedUTC = latestFeed.created_at;
var lastUpdatedIST = convertUTCtoIST(lastUpdatedUTC);
var [date, currtime] = lastUpdatedIST.split(",");
currtime = currtime.trim();
// Determine the next stop
var next_stop = stops[0].name;
for (let i = 0; i < stops.length - 1; i++) {
if (isTimeInInterval(currtime, stops[i].time, stops[i + 1].time)) {
next_stop = stops[i + 1].name;
}
}
let flag=0;
if (timeToSeconds(currtime) > timeToSeconds(stops[stops.length - 1].time) || timeToSeconds(currtime)<timeToSeconds(stops[0].time)) {
next_stop = "Shuttle service is closed ,The next bus will start from 8:00 AM tomorrow";
flag=1;
}
console.log("Next Stop:", next_stop);
// Update marker on the map
if (marker) {
map.removeLayer(marker);
}
var emojiIcon = L.divIcon({
html: '<div style="font-size: 24px; line-height: 30px; text-align: center;">🚍</div>',
className: 'emoji-marker',
iconSize: [45, 45],
iconAnchor: [15, 15] // Centers the icon on the location
});
marker = L.marker([lat, lon], { icon: emojiIcon }).addTo(map);
map.setView([lat, lon], 18);
// Update the "Last Updated" text
document.getElementById("lastUpdate").innerText = lastUpdatedIST;
document.getElementById("Speed").innerText = spd + " km/h";
let ns=document.getElementById("Nextstop");
ns.innerText = next_stop;
let hd=document.getElementById("heading");
if(flag)
{
console.log(flag);
hd.classList.add("yes");
ns.style.color="#de595b";
}
})
.catch(error => {
console.error('Fetch Error:', error);
document.getElementById("lastUpdate").innerText = "Error loading update.";
document.getElementById("Speed").innerText = "Error calculating distance.";
});
}
// Initial fetch on page load and update every minute
updateMapData();
setInterval(updateMapData, 60000);
</script>
</body>
</html>