-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommute.html
644 lines (568 loc) · 48.7 KB
/
commute.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>KTM Train Map</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<style>
body {
font-family: 'Segoe UI Emoji', 'Noto Color Emoji', sans-serif;
word-wrap: break-word;
overflow-wrap: break-word;
white-space: normal;
}
.vehicle-arrow-icon {
font-family: 'Segoe UI Emoji', 'Noto Color Emoji', sans-serif;
}
#map { height: 600px; width: 98%; }
.leaflet-tooltip.vehicle-label {
white-space: nowrap;
font-size: small;
pointer-events: auto;
}
.custom-station-marker .marker-container {
width: 20px;
height: 20px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-size: 12px;
font-weight: bold;
border: 1px solid black;
/*box-shadow: 0 0 5px rgba(0, 0, 0, 0.5); Optional: adds a shadow */
}
.custom-station-marker .marker-number {
line-height: 1;
}
</style>
</head>
<body>
<b>Trains:</b>
<a href="index.html?filter=both">Total</a>:<span id="trainCountBoth">0</span>,
<a href='index.html?filter=up'>Up</a>:<span id="trainCountUp">0</span>,
<a href="index.html?filter=down">Down</a>:<span id="trainCountDown">0</span>
<span id="trainCountFocus"></span>.
<span id="system_status"></span>
<button id="countdownButton" onclick="fetchMtrecTrainPositionApiData()">Refresh in 00s</button>
<button id="toggleButton" onclick="toggleTrainScheduleTableVisibility()">Show Schedule</button>
<div id="map"></div>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
<script> // Data Structures
// Vehicle IDs and Departure Times for 2 KTM stations as of 02-Dec, 2024. https://www.ktmb.com.my/TrainTime.html
// This includes weekday(#2002-#2071) & weekend(#2206-#2271) trains
// Up trains have even vehicleId & Down trains have odd.
let KTMTrains = [
{ vehicleId: 9999, baseStation: "Dummy KTM" , departureTime: "na" , typeOfDay: "na" , direction: "na" , startStation: "na" , vehicleType: "na", isActive: false},
{ vehicleId: 2002, baseStation: "Serdang KTM" , departureTime: "06:30", typeOfDay: "Weekday", direction: "up" , startStation: "Pulau Sebang KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2004, baseStation: "Serdang KTM" , departureTime: "06:55", typeOfDay: "Weekday", direction: "up" , startStation: "Sungai Gadut KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2602, baseStation: "Dummy KTM" , departureTime: "na" , typeOfDay: "Weekday", direction: "up" , startStation: "Pulau Sebang KTM", vehicleType: "LS", isActive: false},
{ vehicleId: 2008, baseStation: "Serdang KTM" , departureTime: "07:25", typeOfDay: "Weekday", direction: "up" , startStation: "Pulau Sebang KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2010, baseStation: "Serdang KTM" , departureTime: "07:58", typeOfDay: "Weekday", direction: "up" , startStation: "Pulau Sebang KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2012, baseStation: "Serdang KTM" , departureTime: "08:30", typeOfDay: "Weekday", direction: "up" , startStation: "Pulau Sebang KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2014, baseStation: "Serdang KTM" , departureTime: "09:02", typeOfDay: "Weekday", direction: "up" , startStation: "Sungai Gadut KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2016, baseStation: "Serdang KTM" , departureTime: "09:32", typeOfDay: "Weekday", direction: "up" , startStation: "Bangi KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2018, baseStation: "Serdang KTM" , departureTime: "10:08", typeOfDay: "Weekday", direction: "up" , startStation: "Kajang KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2020, baseStation: "Serdang KTM" , departureTime: "11:03", typeOfDay: "Weekday", direction: "up" , startStation: "Sungai Gadut KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2024, baseStation: "Serdang KTM" , departureTime: "12:18", typeOfDay: "Weekday", direction: "up" , startStation: "Sungai Gadut KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2028, baseStation: "Serdang KTM" , departureTime: "13:18", typeOfDay: "Weekday", direction: "up" , startStation: "Pulau Sebang KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2032, baseStation: "Serdang KTM" , departureTime: "14:18", typeOfDay: "Weekday", direction: "up" , startStation: "Bangi KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2036, baseStation: "Serdang KTM" , departureTime: "15:23", typeOfDay: "Weekday", direction: "up" , startStation: "Sungai Gadut KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2040, baseStation: "Serdang KTM" , departureTime: "16:28", typeOfDay: "Weekday", direction: "up" , startStation: "Sungai Gadut KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2046, baseStation: "Serdang KTM" , departureTime: "17:18", typeOfDay: "Weekday", direction: "up" , startStation: "Bangi KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2048, baseStation: "Serdang KTM" , departureTime: "17:48", typeOfDay: "Weekday", direction: "up" , startStation: "Sungai Gadut KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2050, baseStation: "Serdang KTM" , departureTime: "18:28", typeOfDay: "Weekday", direction: "up" , startStation: "Pulau Sebang KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2054, baseStation: "Serdang KTM" , departureTime: "19:13", typeOfDay: "Weekday", direction: "up" , startStation: "Bangi KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2058, baseStation: "Serdang KTM" , departureTime: "20:03", typeOfDay: "Weekday", direction: "up" , startStation: "Bangi KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2062, baseStation: "Serdang KTM" , departureTime: "21:06", typeOfDay: "Weekday", direction: "up" , startStation: "Sungai Gadut KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2066, baseStation: "Serdang KTM" , departureTime: "22:16", typeOfDay: "Weekday", direction: "up" , startStation: "Sungai Gadut KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2070, baseStation: "Serdang KTM" , departureTime: "22:48", typeOfDay: "Weekday", direction: "up" , startStation: "Sungai Gadut KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2206, baseStation: "Serdang KTM" , departureTime: "07:25", typeOfDay: "Weekend", direction: "up" , startStation: "Pulau Sebang KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2208, baseStation: "Serdang KTM" , departureTime: "07:58", typeOfDay: "Weekend", direction: "up" , startStation: "Pulau Sebang KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2210, baseStation: "Serdang KTM" , departureTime: "08:30", typeOfDay: "Weekend", direction: "up" , startStation: "Pulau Sebang KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2212, baseStation: "Serdang KTM" , departureTime: "09:02", typeOfDay: "Weekend", direction: "up" , startStation: "Sungai Gadut KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2214, baseStation: "Serdang KTM" , departureTime: "09:32", typeOfDay: "Weekend", direction: "up" , startStation: "Bangi KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2216, baseStation: "Serdang KTM" , departureTime: "10:08", typeOfDay: "Weekend", direction: "up" , startStation: "Bangi KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2220, baseStation: "Serdang KTM" , departureTime: "11:03", typeOfDay: "Weekend", direction: "up" , startStation: "Sungai Gadut KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2224, baseStation: "Serdang KTM" , departureTime: "12:18", typeOfDay: "Weekend", direction: "up" , startStation: "Sungai Gadut KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2228, baseStation: "Serdang KTM" , departureTime: "13:18", typeOfDay: "Weekend", direction: "up" , startStation: "Pulau Sebang KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2232, baseStation: "Serdang KTM" , departureTime: "14:18", typeOfDay: "Weekend", direction: "up" , startStation: "Bangi KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2236, baseStation: "Serdang KTM" , departureTime: "15:23", typeOfDay: "Weekend", direction: "up" , startStation: "Sungai Gadut KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2240, baseStation: "Serdang KTM" , departureTime: "16:28", typeOfDay: "Weekend", direction: "up" , startStation: "Sungai Gadut KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2246, baseStation: "Serdang KTM" , departureTime: "17:18", typeOfDay: "Weekend", direction: "up" , startStation: "Bangi KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2248, baseStation: "Serdang KTM" , departureTime: "17:48", typeOfDay: "Weekend", direction: "up" , startStation: "Sungai Gadut KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2250, baseStation: "Serdang KTM" , departureTime: "18:28", typeOfDay: "Weekend", direction: "up" , startStation: "Pulau Sebang KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2254, baseStation: "Serdang KTM" , departureTime: "19:13", typeOfDay: "Weekend", direction: "up" , startStation: "Bangi KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2258, baseStation: "Serdang KTM" , departureTime: "20:03", typeOfDay: "Weekend", direction: "up" , startStation: "Bangi KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2262, baseStation: "Serdang KTM" , departureTime: "21:06", typeOfDay: "Weekend", direction: "up" , startStation: "Sungai Gadut KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2266, baseStation: "Serdang KTM" , departureTime: "22:16", typeOfDay: "Weekend", direction: "up" , startStation: "Sungai Gadut KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2270, baseStation: "Serdang KTM" , departureTime: "22:48", typeOfDay: "Weekend", direction: "up" , startStation: "Sungai Gadut KTM", vehicleType: "" , isActive: false},
{ vehicleId: 2003, baseStation: "Midvalley KTM", departureTime: "05:39", typeOfDay: "Weekday", direction: "down", startStation: "Sentul KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2007, baseStation: "Midvalley KTM", departureTime: "06:40", typeOfDay: "Weekday", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2011, baseStation: "Midvalley KTM", departureTime: "07:35", typeOfDay: "Weekday", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2013, baseStation: "Midvalley KTM", departureTime: "08:05", typeOfDay: "Weekday", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2015, baseStation: "Midvalley KTM", departureTime: "08:35", typeOfDay: "Weekday", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2017, baseStation: "Midvalley KTM", departureTime: "09:08", typeOfDay: "Weekday", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2019, baseStation: "Midvalley KTM", departureTime: "09:35", typeOfDay: "Weekday", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2021, baseStation: "Midvalley KTM", departureTime: "10:05", typeOfDay: "Weekday", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2025, baseStation: "Midvalley KTM", departureTime: "10:35", typeOfDay: "Weekday", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2027, baseStation: "Midvalley KTM", departureTime: "11:35", typeOfDay: "Weekday", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2031, baseStation: "Midvalley KTM", departureTime: "12:40", typeOfDay: "Weekday", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2035, baseStation: "Midvalley KTM", departureTime: "13:45", typeOfDay: "Weekday", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2039, baseStation: "Midvalley KTM", departureTime: "14:45", typeOfDay: "Weekday", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2045, baseStation: "Midvalley KTM", departureTime: "15:45", typeOfDay: "Weekday", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2049, baseStation: "Midvalley KTM", departureTime: "16:45", typeOfDay: "Weekday", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2053, baseStation: "Midvalley KTM", departureTime: "17:35", typeOfDay: "Weekday", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2603, baseStation: "Midvalley KTM", departureTime: "18:00", typeOfDay: "Weekday", direction: "down", startStation: "Sentul KTM" , vehicleType: "LS", isActive: false},
{ vehicleId: 2057, baseStation: "Midvalley KTM", departureTime: "18:10", typeOfDay: "Weekday", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2059, baseStation: "Midvalley KTM", departureTime: "18:40", typeOfDay: "Weekday", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2061, baseStation: "Midvalley KTM", departureTime: "19:20", typeOfDay: "Weekday", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2063, baseStation: "Midvalley KTM", departureTime: "20:35", typeOfDay: "Weekday", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2067, baseStation: "Midvalley KTM", departureTime: "21:48", typeOfDay: "Weekday", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2071, baseStation: "Midvalley KTM", departureTime: "22:48", typeOfDay: "Weekday", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2207, baseStation: "Midvalley KTM", departureTime: "06:40", typeOfDay: "Weekend", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2211, baseStation: "Midvalley KTM", departureTime: "07:35", typeOfDay: "Weekend", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2213, baseStation: "Midvalley KTM", departureTime: "08:05", typeOfDay: "Weekend", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2215, baseStation: "Midvalley KTM", departureTime: "08:35", typeOfDay: "Weekend", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2219, baseStation: "Midvalley KTM", departureTime: "09:35", typeOfDay: "Weekend", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2223, baseStation: "Midvalley KTM", departureTime: "10:35", typeOfDay: "Weekend", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2225, baseStation: "Midvalley KTM", departureTime: "11:35", typeOfDay: "Weekend", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2229, baseStation: "Midvalley KTM", departureTime: "12:40", typeOfDay: "Weekend", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2233, baseStation: "Midvalley KTM", departureTime: "13:45", typeOfDay: "Weekend", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2237, baseStation: "Midvalley KTM", departureTime: "14:45", typeOfDay: "Weekend", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2243, baseStation: "Midvalley KTM", departureTime: "15:45", typeOfDay: "Weekend", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2247, baseStation: "Midvalley KTM", departureTime: "16:45", typeOfDay: "Weekend", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2251, baseStation: "Midvalley KTM", departureTime: "17:35", typeOfDay: "Weekend", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2253, baseStation: "Midvalley KTM", departureTime: "18:10", typeOfDay: "Weekend", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2257, baseStation: "Midvalley KTM", departureTime: "18:40", typeOfDay: "Weekend", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2259, baseStation: "Midvalley KTM", departureTime: "19:20", typeOfDay: "Weekend", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2261, baseStation: "Midvalley KTM", departureTime: "20:35", typeOfDay: "Weekend", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2265, baseStation: "Midvalley KTM", departureTime: "21:48", typeOfDay: "Weekend", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false},
{ vehicleId: 2271, baseStation: "Midvalley KTM", departureTime: "22:48", typeOfDay: "Weekend", direction: "down", startStation: "Batu Caves KTM" , vehicleType: "" , isActive: false}
];
// Used color blue for Serdang(Up) & red for Midvalley(Down) trains, with a lighter shade used for stations in those directions
// distanceInMins field is updated w.r.t. the 2 Base stations in the respective direction
let KTMStations = {
'Dummy KTM' : {stationId:00000, colour:'green' , distanceInMins:-1 , tripDurationInMins:-1 , tripDistanceInKms:0 ,location: [0.00, 0.00] },
'Batu Caves KTM' : {stationId:50600, colour:'#f97e89', distanceInMins:38 , tripDurationInMins:0 , tripDistanceInKms:0 ,location: [3.237796,101.681215] },
'Taman Wahyu KTM' : {stationId:50500, colour:'#f97e89', distanceInMins:34 , tripDurationInMins:4 , tripDistanceInKms:2.95 ,location: [3.214510,101.672178] },
'Kampung Batu KTM' : {stationId:50400, colour:'#f97e89', distanceInMins:31 , tripDurationInMins:6 , tripDistanceInKms:4.2 ,location: [3.204892,101.675584] },
'Batu Kentonmen KTM': {stationId:50300, colour:'#f97e89', distanceInMins:29 , tripDurationInMins:8 , tripDistanceInKms:5.15 ,location: [3.198485,101.681138] },
'Sentul KTM' : {stationId:50000, colour:'#f97e89', distanceInMins:25 , tripDurationInMins:12 , tripDistanceInKms:7.3 ,location: [3.183581,101.688802] },
'Putra KTM' : {stationId:18800, colour:'#f97e89', distanceInMins:19 , tripDurationInMins:18 , tripDistanceInKms:9.646 ,location: [3.165399,101.691101] },
'Bank Negara KTM' : {stationId:18900, colour:'#f97e89', distanceInMins:16 , tripDurationInMins:21 , tripDistanceInKms:11.006 ,location: [3.155105,101.693118] },
'Kuala Lumpur KTM' : {stationId:19000, colour:'#f97e89', distanceInMins:12 , tripDurationInMins:25 , tripDistanceInKms:12.726 ,location: [3.139444,101.693333] },
'KL Sentral KTM' : {stationId:19100, colour:'#f97e89', distanceInMins:7 , tripDurationInMins:29 , tripDistanceInKms:13.886 ,location: [3.134167,101.686111] },
'Midvalley KTM' : {stationId:19205, colour:'red' , distanceInMins:0 , tripDurationInMins:36 , tripDistanceInKms:16.006 ,location: [3.119211,101.678865] }, //Base for down train
'Seputeh KTM' : {stationId:19300, colour:'#ffffcc', distanceInMins:-1 , tripDurationInMins:39 , tripDistanceInKms:16.706 ,location: [3.113612,101.681474] },
'Salak Selatan KTM' : {stationId:19400, colour:'#ffffcc', distanceInMins:-1 , tripDurationInMins:44 , tripDistanceInKms:20.006 ,location: [3.098413,101.705055] },
'BTS KTM' : {stationId:19600, colour:'#ffffcc', distanceInMins:-1 , tripDurationInMins:50 , tripDistanceInKms:23.106 ,location: [3.076229,101.711119] },
'Serdang KTM' : {stationId:19900, colour:'blue' , distanceInMins:0 , tripDurationInMins:57 , tripDistanceInKms:29.446 ,location: [3.023404,101.716056] }, //Base for up trains
'Kajang KTM' : {stationId:20400, colour:'#8b81fb', distanceInMins:12 , tripDurationInMins:69 , tripDistanceInKms:40.346 ,location: [2.983222,101.790669] },
'Kajang 2 KTM' : {stationId:20402, colour:'#8b81fb', distanceInMins:15 , tripDurationInMins:72 , tripDistanceInKms:42.806 ,location: [2.962640,101.792070] },
'UKM KTM' : {stationId:20500, colour:'#8b81fb', distanceInMins:18 , tripDurationInMins:75 , tripDistanceInKms:45.266 ,location: [2.939775,101.787623] },
'Bangi KTM' : {stationId:20900, colour:'#8b81fb', distanceInMins:26 , tripDurationInMins:81 , tripDistanceInKms:50.066 ,location: [2.904467,101.785943] },
'Batang Benar KTM' : {stationId:21300, colour:'#8b81fb', distanceInMins:39 , tripDurationInMins:94 , tripDistanceInKms:60.516 ,location: [2.829904,101.826655] },
'Nilai KTM' : {stationId:21500, colour:'#8b81fb', distanceInMins:45 , tripDurationInMins:100, tripDistanceInKms:64.416 ,location: [2.802356,101.799303] },
'Labu KTM' : {stationId:22000, colour:'#8b81fb', distanceInMins:59 , tripDurationInMins:110, tripDistanceInKms:71.476 ,location: [2.754501,101.826656] },
'Tiroi KTM' : {stationId:22400, colour:'#8b81fb', distanceInMins:65 , tripDurationInMins:116, tripDistanceInKms:77.226 ,location: [2.741459,101.871914] },
'Seremban KTM' : {stationId:22700, colour:'#8b81fb', distanceInMins:73 , tripDurationInMins:124, tripDistanceInKms:86.176 ,location: [2.719169,101.940792] },
'Senawang KTM' : {stationId:22900, colour:'#8b81fb', distanceInMins:78 , tripDurationInMins:130, tripDistanceInKms:90.846 ,location: [2.690138,101.972336] },
'Sungai Gadut KTM' : {stationId:23100, colour:'#8b81fb', distanceInMins:84 , tripDurationInMins:135, tripDistanceInKms:95.306 ,location: [2.660898,101.996158] },
'Rembau KTM' : {stationId:23900, colour:'#8b81fb', distanceInMins:97 , tripDurationInMins:148, tripDistanceInKms:109.486,location: [2.593055,102.094653] },
'Pulau Sebang KTM' : {stationId:25100, colour:'#8b81fb', distanceInMins:113, tripDurationInMins:165, tripDistanceInKms:134.446,location: [2.46396,102.2263080] }
};
const iconDesigns = {
'up' : { svgPathd: 'M12 2l7 12h-14z' , colour:'blue' },
'down' : { svgPathd: 'M12 22l7-12h-14z' , colour:'red' },
'na' : { svgPathd: 'M2 2 H10 V10 H2 Z' , colour:'green' }
}
</script>
<script> // Functions
function findDeviceTypeBeingUsed() {
const userAgent = navigator.userAgent || navigator.vendor || window.opera;
if (/watch|samsungbrowser/i.test(userAgent.toLowerCase())) { return "Watch";}
if (/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(userAgent.toLowerCase())) { return "Mobile"; }
return "Desktop";
}
function initializePageParameters(){
//Get the filter parameter ('up', 'down', 'both' , 'all' or anything else) from the URL
const urlParams = new URLSearchParams(window.location.search);
const filterParam = urlParams.get('filter');
const focusVehicleIdParamRaw = urlParams.get('focusVehicleId');
let focusVehicleIdParam;
if (!isNaN(focusVehicleIdParamRaw) )
focusVehicleIdParam= parseInt(focusVehicleIdParamRaw, 10);
else
focusVehicleIdParam = 0;
console.log(`filter parameter on URL is ${filterParam} , and focusVehicleId is ${focusVehicleIdParam} `);
return { filterParam, focusVehicleIdParam};
}
function initializeMap() {
// Initialize the map to Location between 2 stations & add OpenStreetMap tile layer
// API reference https://leafletjs.com/reference.html
KTMStations['Dummy KTM'].location = [ (KTMStations['Serdang KTM'].location[0] + KTMStations['Midvalley KTM'].location[0])/2,
(KTMStations['Serdang KTM'].location[1] + KTMStations['Midvalley KTM'].location[1])/2];
const map = L.map('map').setView(KTMStations['Dummy KTM'].location, 11);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18,
}).addTo(map);
return map;
}
function plotStationsOnMap() {
//Remove all stations (if any)
map.eachLayer(layer => {
if (layer instanceof L.Marker && ! layer.isDynamicMarker) {
map.removeLayer(layer);
}
});
for (const [stationName, stationInfo] of Object.entries(KTMStations)) {
if (stationName !== 'Dummy KTM') {
const distanceText = stationInfo.distanceInMins >= 0 ? stationInfo.distanceInMins.toString().padStart(2, '0') : '--';
const stationMarker = L.marker(stationInfo.location, {
icon: L.divIcon({
className: 'custom-station-marker',
html: `<div class="marker-container" style="background-color: ${stationInfo.colour};">
<span class="marker-number">${distanceText}</span>
</div>`,
iconSize: [16, 16],
iconAnchor: [8, 8],
})
}).addTo(map);
let label = `<a href="https://myrailtime.ktmb.com.my/timetable?origin=${stationInfo.stationId}" target="_blank">${stationName}</a>`;
label += stationInfo.distanceInMins >= 0 ? ` (+${stationInfo.distanceInMins} min)` : '';
stationMarker.bindPopup(label, {
permanent: false,
direction: 'left',
offset: [-10, 0],
className: 'custom-popup',
});
}
}
}
async function fetchMtrecTrainPositionApiData() {
// Function to fetch data from MTREC API
// API : https://www.mtrec.name.my/api.html
// DataSets : https://developer.data.gov.my/static-api/data-catalogue
countdown = COUNTDOWN_SECONDS;
try {
const response = await fetch('https://api.mtrec.name.my/api/position?agency=ktmb');
const data = await response.json();
console.log((new Date()).toLocaleTimeString(),'Count:', data.data.length,', (API Response) :', data);
const vehicles = data.data || [];
if (data.data.length == 0 ) {
fetchMtrecStatusMonitorApiData();
} else {
classifyVehiclesForPlotting(vehicles);
}
} catch (error) {
console.error('Error fetching data from API:', error);
fetchMtrecStatusMonitorApiData();
}
}
async function fetchMtrecStatusMonitorApiData() {
try {
const response = await fetch('https://api.mtrec.name.my/api/monitor?service=position-api-ktmb');
const data = await response.json();
console.log((new Date()).toLocaleTimeString(),' (API Response) :', data);
const api_monitor_status = data.data.monitor_status;
document.getElementById("system_status").innerHTML= ("Down"==api_monitor_status) ?
`API Status:<b><span style="color: red;">${api_monitor_status}</span></b>`:
`API Status:<b><span style="color: green;">${api_monitor_status}</span></b>`;
} catch (error) {
console.error('Error fetching data from API:', error);
}
}
function classifyVehiclesForPlotting(vehicles) {
let upCount = 0,downCount = 0,naCount = 0, focusCount = 0;
document.getElementById("trainCountFocus").textContent = "";
unplotAllVechiclesOnMap();
KTMTrains.forEach(train => train.isActive = false);
vehicles.forEach(vehicle => {
const vehicleIdNum = parseInt(vehicle.vehicle.id.split('_')[1] || vehicle.vehicle.id);
let baseKTMStationInfo;
/* plotThisTrain Logic
|Filter | up-Train | down-Train | na-Train|
|-------|------------|------------|---------|
|up | 1 | 0 | 0 |
|down | 0 | 1 | 0 |
|both | 1 | 1 | 0 |
|all | 1 | 1 | 1 | Internal mode for debugging
|ELSE | 1 | 1 | 0 |
*/
let plotThisTrain = true;
let activeKTMTrainInfo = KTMTrains.find(train => train.vehicleId === vehicleIdNum);
if( activeKTMTrainInfo ) {
baseKTMStationInfo = KTMStations[activeKTMTrainInfo.baseStation];
activeKTMTrainInfo.isActive = true;
if ( activeKTMTrainInfo.direction == "up") {
upCount++;
if (filterParam === "down" ) { plotThisTrain = false;}
} else {
downCount++;
if (filterParam === "up" ) { plotThisTrain = false;}
}
} else {
activeKTMTrainInfo = KTMTrains.find(train => train.vehicleId === 9999); //Use Dummy Train
baseKTMStationInfo = KTMStations[activeKTMTrainInfo.baseStation];
naCount++;
if (filterParam !== "all" ) { plotThisTrain = false;}
}
if (focusVehicleIdParam > 0 ) { // If focusVehicleIdParam is used then show just 1 vehicle
if (focusVehicleIdParam == vehicleIdNum) {
focusCount++;
const nearestStations = findNearestStationsOnNorthAndSouth([vehicle.position.latitude, vehicle.position.longitude] );
document.getElementById("trainCountFocus").textContent = `, #${vehicleIdNum} is ${nearestStations}`;
} else {
plotThisTrain = false;
}
}
if (plotThisTrain === true) {
console.log(`Plotting Vehicle: #${vehicleIdNum}, direction ${activeKTMTrainInfo.direction} at Position:(${vehicle.position.latitude}, ${vehicle.position.longitude})`);
plotVehicleOnMap(vehicleIdNum, vehicle.position, baseKTMStationInfo, activeKTMTrainInfo ) ;
} else {
console.log(`\tSkipping Vehicle: #${vehicleIdNum}, direction ${activeKTMTrainInfo.direction} as filterParam is ${filterParam} and focusVehicleId is ${focusVehicleIdParam}`);
}
});
document.getElementById("trainCountUp").textContent = `${upCount}`;
document.getElementById("trainCountDown").textContent = `${downCount}`;
bothCount = upCount + downCount;
allCount = naCount + bothCount;
document.getElementById("trainCountBoth").textContent = `${bothCount}`;
if ( focusVehicleIdParam > 0 && focusCount == 0 ) { // If we are in focus Mode & the focused Train has no data
let focusKTMTrainInfo = KTMTrains.find(train => train.vehicleId === focusVehicleIdParam);
if ( focusKTMTrainInfo ) {
document.getElementById("trainCountFocus").textContent = `, #${focusVehicleIdParam} is not active, it starts from ${focusKTMTrainInfo.startStation}`;
} else {
document.getElementById("trainCountFocus").textContent = `, No information on #${focusVehicleIdParam} `;
}
}
}
function plotVehicleOnMap(vehicleIdNum, vehiclePosition, baseKTMStationInfo, activeKTMTrainInfo) {
vehicleIconDesign= iconDesigns[activeKTMTrainInfo.direction];
const arrowIcon = L.divIcon({
html: `<svg width="30" height="30" xmlns="http://www.w3.org/2000/svg" fill=${vehicleIconDesign.colour} viewBox="0 0 30 30"><path d="${vehicleIconDesign.svgPathd}"/></svg>`,
className: 'vehicle-arrow-icon',
iconSize: [30, 30],
iconAnchor: [12, 12]
});
const marker = L.marker([vehiclePosition.latitude, vehiclePosition.longitude], {
icon: arrowIcon
}).addTo(map);
let label;
let distance;
if ( activeKTMTrainInfo.direction === "na" ) {
distance = 'na';
label = `( #${vehicleIdNum})`;
} else {
distance = calculateDistanceInKM(vehiclePosition.latitude, vehiclePosition.longitude, baseKTMStationInfo.location[0], baseKTMStationInfo.location[1]);
distance = distance.toFixed(2);
label = `${activeKTMTrainInfo.departureTime}( <a href="${window.location.pathname}?focusVehicleId=${vehicleIdNum}" style="text-decoration: none; color: inherit;">#${vehicleIdNum}</a>) - ${distance} km`;
if ( vehicleIdNum === focusVehicleIdParam ) {
map.flyTo([vehiclePosition.latitude, vehiclePosition.longitude]); //, 14, { animation: true }) ;
label = `${label}, ${vehiclePosition.speed} km/h.`;
}
else {
label = `${label}.`;
}
}
marker.bindTooltip(label, {
permanent: true,
direction: 'right',
offset: [10, 0],
className: 'vehicle-label',
interactive: true
});
marker.isDynamicMarker = true; //This marker's location can move on next refresh
}
function unplotAllVechiclesOnMap() {
map.eachLayer(layer => {
if (layer instanceof L.Marker && layer.isDynamicMarker) {
map.removeLayer(layer);
}
});
}
function showTrainScheduleTable(stationName) {
const isWeekend = [0, 6].includes(new Date().getDay());
const typeOfDay = isWeekend ? "Weekend" : "Weekday";
const trainsForStationAndTypeOfDay = KTMTrains.filter(train => train.baseStation === stationName && train.typeOfDay === typeOfDay);
const currentDate = new Date();
if (currentDate.getHours() < 5 ) // Handle times when pages is opened when there are no trains ...
currentDate.setHours(4);
// Create table if there is none. If it has 1 row, append next row to it. If it already had 2 rows then delete the table.
let table = document.querySelector("#trainScheduleTable");
if (table) {
if (table.rows.length === 2) {
table.remove();
table = null;
}
}
if (!table) {
table = document.createElement("table");
table.id = "trainScheduleTable";
table.style.borderCollapse = "collapse";
document.body.appendChild(table);
}
const row = document.createElement("tr");
const stationCell = document.createElement("td");
stationCell.textContent = stationName;
stationCell.style.fontSize = "small";
stationCell.style.border = "1px solid black";
row.appendChild(stationCell);
trainsForStationAndTypeOfDay.forEach(train => {
const cell = document.createElement("td");
cell.style.fontSize = "small"; // Use x-small for smaller font
cell.style.border = "1px solid black";
cell.style.whiteSpace = "normal";
cell.appendChild(document.createTextNode(`${train.departureTime}`));
if (train.isActive == true) {
cell.appendChild(document.createTextNode("*"));
}
cell.appendChild(document.createElement("br"));
cell.appendChild(document.createTextNode("("));
const vehicleLink = document.createElement("a");
vehicleLink.href = `${window.location.pathname}?focusVehicleId=${train.vehicleId}`;
vehicleLink.textContent = `${train.vehicleId}${train.vehicleType}`;
cell.appendChild(vehicleLink);
cell.appendChild(document.createTextNode(")"));
const [hours, minutes] = train.departureTime.split(":").map(Number);
const departureDate = new Date(currentDate);
departureDate.setHours(hours, minutes, 0, 0);
//currentDate.setHours(9, 32, 0, 0); //For TESTING
let diffMinutes = (departureDate - currentDate) / (1000 * 60);
if (diffMinutes < 0 && diffMinutes >= -60) {
cell.style.backgroundColor = "lightcoral";
} else if (diffMinutes >= 0 && diffMinutes <= 120) {
cell.style.backgroundColor = "lightgreen";
}
// Show only the trains for last 1 hour & next 2 hours if the page is opened on a Mobile/Watch.
if ( !(deviceType == "Destop") && ( train.isActive ||(diffMinutes >= -60 && diffMinutes <= 120)) ) {
row.appendChild(cell);
}
if ( (deviceType == "Desktop")) {
row.appendChild(cell);
}
});
table.appendChild(row);
}
function toggleTrainScheduleTableVisibility() {
let tableSchedule = document.querySelector("#trainScheduleTable");
let tableLegend = document.querySelector("#trainScheduleLegend");
const button = document.querySelector("#toggleButton");
if (!tableSchedule) {
showTrainScheduleTable("Serdang KTM");
showTrainScheduleTable("Midvalley KTM");
tableSchedule = document.querySelector("#trainScheduleTable");
tableSchedule.style.display = "none";
tableLegend = document.createElement("table");
tableLegend.id = "trainScheduleLegend";
tableLegend.style.borderCollapse = "collapse";
tableLegend.innerHTML = `
<tr>
<td style="font-size: small; border: 1px dotted black; white-space: normal;">Legend: </td>
<td style="background-color: lightcoral; font-size: small; border: 1px dotted black; white-space: normal;">Train for now - 1 hr</td>
<td style="background-color: lightgreen; font-size: small; border: 1px dotted black; white-space: normal;">Train for now +2 hr</td>
<td style="font-size: small; border: 1px dotted black; white-space: normal;"> * Train is active</td>
</tr>
`;
document.body.appendChild(tableLegend);
};
//if (tableSchedule.style.display === "none") {
if (displayScheduleFlag === false) {
tableSchedule.style.display = "table";
tableLegend.style.display = "table" ;
displayScheduleFlag = true;
tableSchedule.scrollIntoView({ behavior: "smooth", block: "start" }); // Scroll to table
button.textContent = "Hide Schedule";
} else {
tableSchedule.style.display = "none";
tableLegend.style.display = "none" ;
displayScheduleFlag = false;
button.textContent = "Show Schedule";
}
}
function calculateDistanceInKM(lat1, lon1, lat2, lon2) {
// Haversine formula to calculate distance between two coordinates in km
const R = 6371; // Radius of the Earth in KM
const dLat = (lat2 - lat1) * Math.PI / 180;
const dLon = (lon2 - lon1) * Math.PI / 180;
const a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c;
}
function findNearestStationsOnNorthAndSouth(vehiclePosition ) {
let nearestNorthStation = null;
let nearestSouthStation = null;
for (const stationName in KTMStations) {
if (stationName == 'Dummy KTM' ) continue;
const station = KTMStations[stationName];
const distance = calculateDistanceInKM(vehiclePosition[0], vehiclePosition[1], station.location[0], station.location[1]);
if (station.location[0] > vehiclePosition[0]) { // North station
if (!nearestNorthStation || distance < nearestNorthStation.distance) {
nearestNorthStation = { name: stationName, ...station, distance };
}
} else if (station.location[0] < vehiclePosition[0]) { // South station
if (!nearestSouthStation || distance < nearestSouthStation.distance) {
nearestSouthStation = { name: stationName, ...station, distance };
}
}
}
let nearestStations = "";
if ( nearestNorthStation && nearestSouthStation) {
let tripDistanceInKmsBetweenStations = Math.abs(nearestNorthStation.tripDistanceInKms - nearestSouthStation.tripDistanceInKms);
let tripDurationInMinsBetweenStations = Math.abs(nearestNorthStation.distanceInMins - nearestSouthStation.distanceInMins);
if (nearestNorthStation.distanceInMins < nearestSouthStation.distanceInMins) {
tripDurationInMinsBetweenNearestStationAndTrain = (nearestNorthStation.distance / tripDistanceInKmsBetweenStations) * tripDurationInMinsBetweenStations;
trainDurationFromBaseStation = (nearestNorthStation.distanceInMins + tripDurationInMinsBetweenNearestStationAndTrain).toFixed(0) ;
nearestStations = `Approx ${trainDurationFromBaseStation} Mins away, between `;
nearestStations += (nearestNorthStation ? `${nearestNorthStation.name} (${nearestNorthStation.distanceInMins} mins away)` : "no northern station") + " & " +
(nearestSouthStation ? `${nearestSouthStation.name} (${nearestSouthStation.distanceInMins} mins away)` : "no southern station");
}else {
tripDurationInMinsBetweenNearestStationAndTrain = (nearestSouthStation.distance / tripDistanceInKmsBetweenStations) * tripDurationInMinsBetweenStations;
trainDurationFromBaseStation = (nearestSouthStation.distanceInMins + tripDurationInMinsBetweenNearestStationAndTrain).toFixed(0) ;
nearestStations = `Approx ${trainDistanceFromBaseStation} Mins away, between `;
nearestStations += (nearestSouthStation ? `${nearestSouthStation.name} (${nearestSouthStation.distanceInMins} mins away)` : "no southern station") + " & " +
(nearestNorthStation ? `${nearestNorthStation.name} (${nearestNorthStation.distanceInMins} mins away)` : "no northern station");
}
speakDistance(trainDurationFromBaseStation);
}
return nearestStations;
}
function speakDistance(distance) {
// Check if the browser supports speech synthesis
if ('speechSynthesis' in window) {
const message = new SpeechSynthesisUtterance(`Train is ${distance} minutes away.`);
message.lang = 'en-US'; // Set language
message.rate = 1; // Adjust speed if needed (0.5 - 2)
message.pitch = 1; // Adjust pitch if needed (0 - 2)
window.speechSynthesis.speak(message); // Speak the message
} else {
console.log("Speech synthesis not supported in this browser.");
}
}
//MAIN PROCESS STARTS HERE
const COUNTDOWN_SECONDS = 60;
const map = initializeMap();
plotStationsOnMap();
const { filterParam, focusVehicleIdParam } = initializePageParameters();
let displayScheduleFlag = false;
const deviceType = findDeviceTypeBeingUsed();
// if ( deviceType == "Watch") document.getElementById("map").style.display = "none"; ///POC for system without Map, which worked fine
let countdown = COUNTDOWN_SECONDS;
fetchMtrecTrainPositionApiData(); // <== Main Function to fetch data & Plot
setInterval(() => {
countdown -= 1;
const button = document.getElementById("countdownButton");
button.textContent = `Refresh in ${countdown}s`;
if (countdown <= 0) {
fetchMtrecTrainPositionApiData();
}
}, 1000);
</script>
</body>
</html>