-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1349 lines (1282 loc) · 49.1 KB
/
index.html
File metadata and controls
1349 lines (1282 loc) · 49.1 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
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta name="google-site-verification" content="9o55RpXAKN7kR9cYHHvHkWuktV3JVcCYF5mt9XZULCY" />
<meta charset="UTF-8" />
<meta name="author" content="卓稟鈞(AndrewCho)">
<meta name="description" content="一個專為 YouBike 設計的簡單、美觀且低流量站點搜尋器,提供快速查詢站點資訊功能,滿足即時需求,適合所有用戶使用。">
<meta name="keywords" content="YouBike, youBike, YouBike2.0, youBike2.0, ubike, ibike, obike, kbike, 微笑單車,公共自行車, 腳踏車,YouBike站點搜尋, 站點搜尋, YouBike 資訊, YouBike據點,ubike據點 ,附近站點, 附近YouBike站點, 全台YouBike, 台北YouBike, 新北YouBike, 桃園YouBike, 新竹YouBike, 苗栗YouBike, 臺中YouBike, 嘉義YouBike , 臺南YouBike, 高雄YouBike, 屏東YouBike, 臺東YouBike , YouBike即時資訊, 定位功能, github, github.io, youbike線上查詢" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>YouBike 站點搜尋 : 線上YouBike站點搜尋器</title>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="manifest" href="/YouBike/manifest.json" />
<link rel="icon" type="image/x-x-icon" href="https://potatosserver.github.io/YouBike/icons/icon.ico" />
<!-- Leaflet CSS -->
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=" crossorigin=""/>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", "Microsoft JhengHei", "PingFang TC", sans-serif;
margin: 20px;
background: #f4f4f4;
/* 移除 iOS 上的點擊高亮效果 */
-webkit-tap-highlight-color: transparent;
}
/* 移除所有可點擊元素的預設焦點框 */
a:focus, button:focus, input:focus, select:focus, textarea:focus, [tabindex]:focus {
outline: none;
}
h1 {
color: #007BFF;
text-align: center;
margin-bottom: 20px;
}
#searchContainer {
display: flex;
align-items: center;
margin-left: auto;
margin-right: auto;
max-width: 500px;
width: 100%;
position: relative;
padding: 8px 16px;
box-sizing: border-box;
background-color: white;
border-radius: 28px;
transition: box-shadow 0.3s ease;
border: 1.5px solid #007BFF;
}
body.dark-mode #searchContainer {
background-color: #1e1e1e;
border: 1.5px solid #90CAF9;
}
#keyword {
padding: 0 0 0 12px;
flex-grow: 1;
border: none;
border-radius: 0;
margin: 0;
font-size: 16px;
box-shadow: none;
word-wrap: break-word;
overflow-wrap: break-word;
display: block;
background-color: transparent;
color: #333;
}
body.dark-mode #keyword {
color: #ffffff;
}
body.dark-mode #keyword::placeholder {
color: rgba(255, 255, 255, 0.7);
}
#keyword:focus {
outline: none;
}
#searchIcon {
position: static;
margin-left: 10px;
font-size: 24px;
cursor: pointer;
color: #555;
transition: color 0.3s ease;
flex-shrink: 0;
display: inline-flex;
align-items: center;
justify-content: center;
width: 24px;
height: 24px;
}
body.dark-mode #searchIcon {
color: #ccc;
}
#searchIcon:hover {
color: #007BFF;
}
#locationIcon {
margin-left: 10px;
font-size: 20px;
cursor: pointer;
color: #555;
transition: color 0.3s ease;
flex-shrink: 0;
}
body.dark-mode #locationIcon {
color: #ccc;
}
#locationIcon.active {
color: #007BFF;
}
body.dark-mode #locationIcon.active {
color: #90CAF9;
}
button {
padding: 10px 20px;
background-color: #007BFF;
color: white;
border: none;
border-radius: 0 5px 5px 0;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s ease;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
margin-left: 0;
width: auto;
min-width: 50px;
text-align: center;
display: block;
}
button:hover {
background-color: #0056b3;
}
#results {
margin-top: 20px;
display: flex;
flex-wrap: wrap;
gap: 20px;
justify-content: center;
}
.station {
background-color: white;
border: 1px solid #e0e0e0;
padding: 20px;
border-radius: 10px;
margin-bottom: 10px;
width: calc(33% - 20px);
min-width: 250px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
transition: transform 0.2s ease, box-shadow 0.2s ease;
word-wrap: break-word;
overflow-wrap: break-word;
position: relative;
cursor: pointer;
}
.station:hover {
transform: translateY(-5px);
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2);
}
.station h3 {
margin: 0 0 10px 0;
color: #007BFF;
font-size: 1.2em;
word-wrap: break-word;
overflow-wrap: break-word;
padding-right: 30px;
}
.station p {
margin: 0 0 5px 0;
color: #555;
font-size: 1em;
word-wrap: break-word;
overflow-wrap: break-word;
}
.station .pin-button {
position: absolute;
top: 10px;
right: 10px;
font-size: 24px;
cursor: pointer;
color: #007BFF;
transition: transform 0.2s ease, color 0.2s ease;
user-select: none;
}
.station .pin-button:hover {
transform: scale(1.2);
color: #0056b3;
}
.station .pin-button.pinned {
color: #FFD700;
}
#loading {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
padding: 20px;
background: rgba(255, 255, 255, 0.8);
border-radius: 5px;
font-size: 1.2em;
z-index: 10;
display: none;
opacity: 0;
transition: opacity 0.5s ease;
white-space: nowrap;
width: auto;
}
#loading.show {
opacity: 1;
display: block;
}
.overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
z-index: 999;
/* --- 動畫修改 START --- */
opacity: 0;
visibility: hidden;
transition: opacity 0.3s ease, visibility 0.3s ease;
/* --- 動畫修改 END --- */
}
.overlay.show {
/* --- 動畫修改 START --- */
opacity: 1;
visibility: visible;
/* --- 動畫修改 END --- */
}
.modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
z-index: 1000;
text-align: center;
display: none;
}
.modal.show {
display: block;
}
.modal button {
margin: 10px auto;
display: block;
padding: 10px 20px;
background-color: #007BFF;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s ease;
}
.modal button:hover {
background-color: #0056b3;
}
/* --- 地圖彈窗樣式修改 START --- */
#mapModal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) scale(0.95); /* 初始狀態稍微縮小 */
width: 90%;
max-width: 600px;
height: 80%;
max-height: 500px;
background-color: white;
border-radius: 10px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
z-index: 1001;
display: flex; /* 維持 flex 以便內部排版 */
flex-direction: column;
opacity: 0; /* 初始為透明 */
visibility: hidden; /* 初始為隱藏 */
transition: opacity 0.3s ease, transform 0.3s ease, visibility 0.3s; /* 加入過渡動畫 */
}
#mapModal.show {
opacity: 1; /* 顯示時淡入 */
visibility: visible; /* 設為可見 */
transform: translate(-50%, -50%) scale(1); /* 恢復正常大小 */
}
/* --- 地圖彈窗樣式修改 END --- */
#mapContainer {
width: 100%;
height: 100%;
border-radius: 10px; /* 地圖容器的圓角 */
}
/* 關閉按鈕樣式 */
#closeMapBtn {
position: absolute;
top: 8px;
right: 8px;
background: rgba(255, 255, 255, 1); /* 不透明度設定為 100% */
color: #333;
border: 1px solid #ccc;
font-size: 20px;
font-weight: bold;
cursor: pointer;
z-index: 1002;
width: 30px; /* 正方形 */
height: 30px; /* 正方形 */
line-height: 28px;
text-align: center;
border-radius: 10px; /* 與地圖彈窗相同的圓角 */
padding: 0;
}
body.dark-mode #closeMapBtn {
background: #ffffff; /* 不透明度設定為 100% */
color: #333; /* 為了深色模式下的對比度,調整顏色 */
border-color: #ccc;
}
body.dark-mode {
background: #333;
color: #ffffff;
}
body.dark-mode h1 {
color: #90CAF9;
}
body.dark-mode #keyword {
background-color: transparent;
color: #ffffff;
}
body.dark-mode #searchContainer {
background-color: #1e1e1e;
}
body.dark-mode #searchIcon {
color: #ccc;
}
body.dark-mode button {
background-color: #90caf9;
color: #121212;
}
body.dark-mode button:hover {
background-color: #64b5f6;
}
body.dark-mode .station {
background-color: #444;
border-color: #555;
color: #ffffff;
}
body.dark-mode .station h3 {
color: #90CAF9;
}
body.dark-mode .station p {
color: #cccccc;
}
body.dark-mode .modal {
background-color: #333;
color: #ffffff;
}
body.dark-mode .overlay {
background-color: rgba(0, 0, 0, 0.8);
}
body.dark-mode #mapModal {
background-color: #444;
}
body.dark-mode #loading {
background: #ffffff;
color: #000000;
}
body.dark-mode #loadingOverlay {
background-color: rgba(51, 51, 51, 0.9);
}
body.dark-mode #loadingText {
color: #90CAF9;
}
body.dark-mode #noticeBox {
background-color: #000;
color: #000;
border: 1px solid #444;
}
#floatingMenuOptions {
position: fixed;
bottom: 120px;
right: 20px;
display: flex;
flex-direction: column;
gap: 10px;
z-index: 1000; /* 保持原有的 z-index */
opacity: 0;
visibility: hidden;
transition: opacity 0.3s ease, visibility 0.3s ease;
}
#floatingMenuOptions.show {
opacity: 1;
visibility: visible;
}
#floatingMenuOptions a,
#floatingMenuOptions button {
width: 50px;
height: 50px;
background-color: white;
color: black;
border: none;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
text-decoration: none;
transition: background-color 0.3s ease, transform 0.2s ease;
}
#floatingMenuOptions a:hover,
#floatingMenuOptions button:hover {
background-color: #f0f0f0;
transform: scale(1.1);
}
#darkModeToggle {
background: linear-gradient(135deg, #007BFF, #0056b3);
color: white;
font-size: 24px;
}
#darkModeToggle:hover {
background: linear-gradient(135deg, #0056b3, #003f7f);
}
body.dark-mode #darkModeToggle {
background: linear-gradient(135deg, #90caf9, #64b5f6);
color: #121212;
}
body.dark-mode #darkModeToggle:hover {
background: linear-gradient(135deg, #64b5f6, #42a5f5);
}
#mainToggleButton {
position: fixed;
bottom: 60px;
right: 20px;
z-index: 998; /* 將 z-index 調整為更小的值,例如 998 */
width: 50px;
height: 50px;
background: white;
color: black;
border: none;
border-radius: 50%;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
font-size: 24px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
transition: transform 0.2s ease;
}
#mainToggleButton:hover {
transform: scale(1.1);
}
#updateCountdown {
white-space: nowrap;
z-index: 997; /* 將 z-index 調整為更小的值,例如 997 */
}
@media (max-width: 768px) {
.station {
width: calc(50% - 20px);
}
#searchContainer {
padding: 8px 16px;
}
#keyword {
flex-grow: 1;
margin-right: 0;
margin-bottom: 0;
border-radius: 28px;
display: block;
padding: 0 0 0 12px;
}
#searchIcon {
position: static;
margin-left: 10px;
margin-top: 0;
align-self: auto;
font-size: 24px;
width: 24px;
height: 24px;
display: inline-flex;
align-items: center;
justify-content: center;
}
#locationIcon {
margin-left: 10px;
padding-right: 0;
}
button {
width: 100%;
border-radius: 0 0 5px 5px;
display: block;
}
#results {
gap: 10px;
}
.station {
margin-bottom: 10px;
}
}
@media (max-width: 480px) {
.station {
width: 100%;
}
#searchContainer {
padding: 10px 16px;
}
#keyword {
flex-grow: 1;
padding: 0 0 0 12px;
}
}
#loadingOverlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(255, 255, 255, 0.9);
display: flex;
justify-content: center;
align-items: center;
z-index: 10000;
transition: opacity 0.5s ease;
}
#loadingContent {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
#loadingText {
font-size: 1.5em;
color: #007BFF;
text-align: center;
}
</style>
</head>
<body>
<div id="loadingOverlay">
<div id="loadingContent">
<h2 id="loadingText">載入中</h2>
</div>
</div>
<div id="mainContent" style="display:none;">
<h1 id="heading">YouBike 站點搜尋</h1>
<div id="searchContainer">
<input type="text" id="keyword" placeholder="請輸入站點名稱、地址" value="" onfocus="this.placeholder=''" onblur="this.placeholder='請輸入站點名稱、地址'" />
<span id="searchIcon" class="material-icons" title="搜尋">search</span>
<span id="locationIcon" class="material-icons" title="使用定位">my_location</span>
</div>
<div id="results"></div>
<div id="loading">載入中,請稍候...</div>
<div class="overlay"></div>
<div class="modal" id="permissionModal">
<p id="modalText"></p>
<button id="modalButton">確定</button>
</div>
<!-- 地圖彈窗 -->
<div id="mapModal">
<!-- 重新加入關閉按鈕 -->
<button id="closeMapBtn">×</button>
<div id="mapContainer"></div>
</div>
<div id="floatingMenuOptions">
<a href="https://github.com/kevin970712/YouBike" target="_blank" title="YouBike Repo">
<img loading="lazy" src="https://potatosserver.github.io/YouBike/icons/html.webp" alt="YouBike Repo" style="width:30px; height:30px;" />
</a>
<a href="https://github.com/potatosserver/YouBike_Python" target="_blank" title="YouBike Python">
<img loading="lazy" src="https://potatosserver.github.io/YouBike/icons/python.webp" alt="YouBike Python" style="width:30px; height:30px;" />
</a>
<button id="darkModeToggle" title="切換暗色模式">🌙</button>
</div>
<!-- z-index 已調整,使其位於更底層 -->
<button id="mainToggleButton" title="設定" style="position: fixed; bottom: 60px; right: 20px; z-index: 998; width: 50px; height: 50px; background: white; color: black; border: none; border-radius: 50%; cursor: pointer; display: flex; align-items: center; justify-content: center; font-size: 24px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); transition: transform 0.2s ease;">⚙️</button>
</div>
<!-- z-index 已調整,使其位於更底層 -->
<button id="updateCountdown" style="display:none; position: fixed; bottom: 10px; left: 50%; transform: translateX(-50%); z-index: 997; padding: 6px 12px; background-color: white; color: #007bff; border: 2px solid #007bff; border-radius: 25px; font-size: 0.8em; box-shadow: 0 2px 6px rgba(0,0,0,0.2); transition: all 0.3s ease; width: 100px;">60秒後更新</button>
<!-- Leaflet JS -->
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js" integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" crossorigin=""></script>
<script>
let userLocation = null;
let sortingLocked = false;
let initialLoad = true;
let locationDenied = false;
let geolocationErrorMessage = null;
let dataLoaded = false;
const defaultLatitude = 22.6315725;
const defaultLongitude = 120.3025079;
const overlay = document.querySelector('.overlay');
const modal = document.querySelector('.modal');
const modalText = document.getElementById('modalText');
const modalButton = document.getElementById('modalButton');
const resultsDiv = document.getElementById('results');
const loadingDiv = document.getElementById('loading');
const keywordInput = document.getElementById('keyword');
const maxResults = 100;
const defaultSearchArea = "";
const pinnedStations = new Set(JSON.parse(localStorage.getItem('pinnedStations') || '[]').map(id => String(id)));
let useLocation = localStorage.getItem("useLocation") === "true";
const locationIcon = document.getElementById('locationIcon');
// 地圖相關變數
const mapModal = document.getElementById('mapModal');
// 重新獲取關閉按鈕元素
const closeMapBtn = document.getElementById('closeMapBtn');
const mapContainer = document.getElementById('mapContainer');
let map = null;
if (useLocation) {
locationIcon.classList.add('active');
} else {
locationIcon.classList.remove('active');
}
locationIcon.addEventListener('click', () => {
useLocation = !useLocation;
localStorage.setItem("useLocation", useLocation);
if (useLocation) {
locationIcon.classList.add('active');
sortingLocked = true;
getLocation();
} else {
locationIcon.classList.remove('active');
userLocation = { latitude: defaultLatitude, longitude: defaultLongitude };
searchYouBike();
updateVehicleData();
setTimeout(updateResultsOrder, 1500);
}
});
// --- 顯示地圖的函式修改 START ---
function showMap(lat, lng, name) {
// 使用 classList.add 來觸發 CSS 動畫
mapModal.classList.add('show');
overlay.classList.add('show');
if (map) {
map.remove();
}
const tileUrl = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
const attribution = '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors';
map = L.map(mapContainer).setView([lat, lng], 19);
L.tileLayer(tileUrl, {
attribution: attribution,
maxZoom: 19
}).addTo(map);
L.marker([lat, lng]).addTo(map)
.bindPopup(name)
.openPopup();
// 確保地圖在彈窗動畫結束後正確渲染
setTimeout(() => {
map.invalidateSize();
}, 100);
// 當地圖彈窗開啟時,新增一個歷史紀錄狀態
history.pushState({ mapOpen: true }, '', '#map');
}
// --- 顯示地圖的函式修改 END ---
// --- 關閉地圖的函式修改 START ---
function closeMap() {
// 使用 classList.remove 來觸發 CSS 動畫
mapModal.classList.remove('show');
overlay.classList.remove('show');
}
// --- 關閉地圖的函式修改 END ---
// 為關閉按鈕添加事件監聽
closeMapBtn.addEventListener('click', () => {
// 如果當前的歷史狀態是地圖彈窗開啟的狀態,則返回上一個歷史狀態
if (history.state && history.state.mapOpen) {
history.back();
} else {
closeMap();
}
});
// --- 遮罩層點擊事件修改 START ---
overlay.addEventListener('click', (event) => {
// 檢查 mapModal 是否可見 (透過 class)
if(mapModal.classList.contains('show')) {
// 如果當前的歷史狀態是地圖彈窗開啟的狀態,則返回上一個歷史狀態,這會觸發 popstate 事件來關閉地圖
if (history.state && history.state.mapOpen) {
history.back();
} else {
closeMap();
}
} else if (modal.classList.contains('show')) {
hideModal();
}
});
// --- 遮罩層點擊事件修改 END ---
// 使用事件代理來處理站點卡片的點擊事件
resultsDiv.addEventListener('click', (event) => {
const stationCard = event.target.closest('.station');
if (stationCard) {
// 確保點擊的不是釘選按鈕
if(event.target.classList.contains('pin-button')) {
return;
}
const lat = parseFloat(stationCard.dataset.lat);
const lng = parseFloat(stationCard.dataset.lng);
const name = stationCard.querySelector('h3').textContent;
showMap(lat, lng, name);
}
});
// --- 監聽 popstate 事件修改 START ---
window.addEventListener('popstate', (event) => {
// 如果地圖彈窗是開啟的 (透過 class 判斷),就關閉它
if (mapModal.classList.contains('show')) {
closeMap();
}
});
// --- 監聽 popstate 事件修改 END ---
function togglePinStation(stationId, event) {
event.stopPropagation();
const id = String(stationId);
if (pinnedStations.has(id)) {
pinnedStations.delete(id);
} else {
pinnedStations.add(id);
}
try {
localStorage.setItem('pinnedStations', JSON.stringify([...pinnedStations]));
} catch (e) {
console.error('Failed to save pinned stations:', e);
}
const pinButton = document.querySelector(`.pin-button[data-id="${id}"]`);
if (pinButton) {
const isPinned = pinnedStations.has(id);
pinButton.classList.toggle('pinned', isPinned);
pinButton.textContent = isPinned ? '★' : '☆';
}
updateResultsOrder();
}
let mainContentShown = false;
function showMainContent() {
if (!mainContentShown) {
const overlay = document.getElementById("loadingOverlay");
const mainContent = document.getElementById("mainContent");
overlay.style.opacity = "0";
setTimeout(() => {
overlay.style.display = "none";
mainContent.style.display = "block";
document.getElementById('updateCountdown').style.display = "block";
}, 500);
mainContentShown = true;
}
}
(function simulateLoadingPercentage() {
const loadingText = document.getElementById("loadingText");
let prefix = "載入中:";
let progress = 0;
let lockedProgress = null;
loadingTextInterval = setInterval(() => {
if (!mainContentShown) {
if (progress < 85) {
progress++;
loadingText.textContent = prefix + progress + "%";
} else {
if (lockedProgress === null) {
lockedProgress = 85 + Math.floor(Math.random() * 11);
}
loadingText.textContent = prefix + lockedProgress + "%";
}
} else {
clearInterval(loadingTextInterval);
}
}, 50);
})();
let youbikeDataCache = null;
async function searchYouBike(showLoadingIndicator = true) {
if (showLoadingIndicator) showLoading();
const keyword = keywordInput.value.trim().toLowerCase();
try {
if (!youbikeDataCache) {
const youbikeUrl = "https://apis.youbike.com.tw/json/station-min-yb2.json";
const response = await fetch(youbikeUrl, {
headers : {
'accept': '*/*',
'accept-language': 'zh-TW,zh;q=0.9',
}
});
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
const youbikeData = await response.json();
youbikeDataCache = Array.isArray(youbikeData)
? youbikeData.map(item => ({ ...item, lat: parseFloat(item.lat), lng: parseFloat(item.lng) }))
: [];
}
let youbikeResults = youbikeDataCache;
if (keyword) {
youbikeResults = youbikeResults.filter(station => {
const stationNameTW = station.name_tw ? station.name_tw.toLowerCase() : '';
const stationAddressTW = station.address_tw ? station.address_tw.toLowerCase() : '';
return stationNameTW.includes(keyword) || stationAddressTW.includes(keyword);
});
}
youbikeResults = youbikeResults.sort((a, b) => {
const distA = calculateDistance(
userLocation ? userLocation.latitude : defaultLatitude,
userLocation ? userLocation.longitude : defaultLongitude,
a.lat, a.lng
);
const distB = calculateDistance(
userLocation ? userLocation.latitude : defaultLatitude,
userLocation ? userLocation.longitude : defaultLongitude,
b.lat, b.lng
);
return distA - distB;
});
let limit = keyword ? 50 : 20;
let usedStations;
if (!keyword) {
const pinnedFromLocal = youbikeResults.filter(station => pinnedStations.has(String(station.station_no)));
const unpinned = youbikeResults.filter(station => !pinnedStations.has(String(station.station_no)));
pinnedFromLocal.sort((a, b) => {
const distA = calculateDistance(
userLocation ? userLocation.latitude : defaultLatitude,
userLocation ? userLocation.longitude : defaultLongitude,
a.lat, a.lng
);
const distB = calculateDistance(
userLocation ? userLocation.latitude : defaultLatitude,
userLocation ? userLocation.longitude : defaultLongitude,
b.lat, b.lng
);
return distA - distB;
});
unpinned.sort((a, b) => {
const distA = calculateDistance(
userLocation ? userLocation.latitude : defaultLatitude,
userLocation ? userLocation.longitude : defaultLongitude,
a.lat, a.lng
);
const distB = calculateDistance(
userLocation ? userLocation.latitude : defaultLatitude,
userLocation ? userLocation.longitude : defaultLongitude,
b.lat, b.lng
);
return distA - distB;
});
usedStations = [...pinnedFromLocal, ...unpinned].slice(0, limit);
} else {
usedStations = youbikeResults.slice(0, limit);
const pinnedList = usedStations.filter(station => pinnedStations.has(String(station.station_no)));
const nonPinnedList = usedStations.filter(station => !pinnedStations.has(String(station.station_no)));
pinnedList.sort((a, b) => {
const distA = calculateDistance(
userLocation ? userLocation.latitude : defaultLatitude,
userLocation ? userLocation.longitude : defaultLongitude,
a.lat, a.lng
);
const distB = calculateDistance(
userLocation ? userLocation.latitude : defaultLatitude,
userLocation ? userLocation.longitude : defaultLongitude,
b.lat, b.lng
);
return distA - distB;
});
nonPinnedList.sort((a, b) => {
const distA = calculateDistance(
userLocation ? userLocation.latitude : defaultLatitude,
userLocation ? userLocation.longitude : defaultLongitude,
a.lat, a.lng
);
const distB = calculateDistance(
userLocation ? userLocation.latitude : defaultLatitude,
userLocation ? userLocation.longitude : defaultLongitude,
b.lat, b.lng
);
return distA - distB;
});
usedStations = [...pinnedList, ...nonPinnedList];
}
const isOnline = navigator.onLine;
let vehData = {};
const stationIds = usedStations.map(station => String(station.station_no));
if (isOnline) {
vehData = await queryVehicleData(stationIds);
}
const organizedOutput = usedStations.map(site => {
const info = {
id: String(site.station_no),
name: site.name_tw,
coords: [site.lat, site.lng],
district: site.district_tw,
address: site.address_tw
};
const vehicle = isOnline ? (Object.entries(vehData).find(([key]) => key === info.id)?.[1] || {}) : {};
return { station_info: info, vehicle_info: vehicle };
});
resultsDiv.innerHTML = '';
organizedOutput.forEach(item => {
const station = item.station_info;
const vehicle = item.vehicle_info;
let card = document.querySelector(`.station[data-id="${station.id}"]`);
const isPinned = pinnedStations.has(String(station.id));
const pinButtonHtml = `<span
class="pin-button ${isPinned ? 'pinned' : ''}"
data-id="${station.id}"
onclick="togglePinStation('${station.id}', event)"
>${isPinned ? '★' : '☆'}</span>`;
const distance = calculateDistance(
userLocation ? userLocation.latitude : defaultLatitude,
userLocation ? userLocation.longitude : defaultLongitude,
station.coords[0],
station.coords[1]
);
const displayDistance = distance < 1 ? Math.round(distance * 1000) + " 公尺"
: distance.toFixed(2) + " 公里";
const distanceLabel = "距離:";
const addressLabel = "地址:";
const slotsLabel = "可停空位數:";
const unknownStation = "未知站點";
const unknownAddress = "未知地址";
const unknownValue = "未知";
const vehicleInfoHtml = isOnline ? `
<p>YouBike 2.0: <span class="vehicle_yb2">${vehicle?.available_2_0 !== undefined ? vehicle.available_2_0 : unknownValue}</span></p>
<p>YouBike 2.0E: <span class="vehicle_ybe">${vehicle?.available_e !== undefined ? vehicle.available_e : unknownValue}</span></p>
<p>${slotsLabel} <span class="vehicle_empty">${vehicle?.empty_spaces !== undefined ? vehicle.empty_spaces : unknownValue}</span></p>
` : '';
const isDefaultLocation = userLocation && userLocation.latitude === defaultLatitude && userLocation.longitude === defaultLongitude;
const distanceHTML = !isDefaultLocation ? `<p class="distance">${distanceLabel} ${displayDistance}</p>` : "";
const cardContent = `
${pinButtonHtml}
<h3>${station.name || unknownStation}</h3>
${distanceHTML}
<p>${addressLabel} ${station.address || unknownAddress}</p>
${vehicleInfoHtml}
`;
card = document.createElement('div');
card.className = 'station';
card.dataset.id = station.id;
card.dataset.lat = station.coords[0];
card.dataset.lng = station.coords[1];
card.style.width = window.innerWidth < 768 ? (window.innerWidth < 480 ? "100%" : "calc(50% - 10px)") : "calc(33% - 14px)";
card.innerHTML = cardContent;
resultsDiv.appendChild(card);
});
updateResultsOrder();
dataLoaded = true;
if (showLoadingIndicator) {
showMainContent();
}
} catch (error) {
console.error("Fetch error:", error);
resultsDiv.innerHTML = `發生錯誤: ${error.message}`;
} finally {
if (showLoadingIndicator) hideLoading();
}
}
async function queryVehicleData(stationIds) {
if (stationIds.length > 60) {
stationIds = stationIds.slice(0, 60);
}
const batchSize = 20;