-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnomap.html
More file actions
1177 lines (1131 loc) · 44.8 KB
/
nomap.html
File metadata and controls
1177 lines (1131 loc) · 44.8 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-icon" href="https://potatosserver.github.io/YouBike/icons/icon.ico" />
<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;
}
h1 {
color: #007BFF;
text-align: center;
margin-bottom: 20px;
}
/* Material 3 風格的搜尋欄容器 */
#searchContainer {
display: flex;
align-items: center;
margin-left: auto;
margin-right: auto;
max-width: 500px;
width: 100%;
position: relative;
padding: 8px 16px; /* 調整內部間距,左右16px */
box-sizing: border-box; /* 確保 padding 不會增加總寬度 */
background-color: white; /* Default background */
border-radius: 28px; /* 大圓角 */
transition: box-shadow 0.3s ease;
border: 1.5px solid #007BFF;
}
body.dark-mode #searchContainer {
background-color: #1e1e1e; /* Dark mode background */
border: 1.5px solid #90CAF9;
}
/* Material 3 風格的搜尋輸入框 */
#keyword {
padding: 0 0 0 12px; /* 調整 padding-left 確保文字不會被遮蓋 */
flex-grow: 1; /* 讓輸入框佔據可用空間 */
border: none; /* 移除邊界 */
border-radius: 0; /* 圓角由 container 控制 */
margin: 0; /* 移除 margin */
font-size: 16px;
box-shadow: none; /* 陰影由 container 控制 */
word-wrap: break-word;
overflow-wrap: break-word;
display: block;
background-color: transparent; /* 透明背景,顯示 container 背景 */
color: #333; /* Default text color */
}
body.dark-mode #keyword {
color: #ffffff; /* Dark mode text color */
}
body.dark-mode #keyword::placeholder {
color: rgba(255, 255, 255, 0.7); /* 較亮的白色,半透明 */
}
#keyword:focus {
outline: none; /* 移除預設 focus 框 */
}
/* 搜尋 icon 樣式 (Material Icons) */
#searchIcon {
position: static; /* 讓 icon 在 flex 佈局中自然排列 */
margin-left: 10px; /* 與 input 之間的間距 */
font-size: 24px; /* Material Icons 預設大小 */
cursor: pointer;
color: #555; /* Default icon color */
transition: color 0.3s ease;
flex-shrink: 0; /* 防止 icon 縮小 */
/* Material Icons 需要這些樣式 */
display: inline-flex; /* 使用 flex 讓 icon 居中 */
align-items: center;
justify-content: center;
width: 24px; /* 設置寬度 */
height: 24px; /* 設置高度 */
}
body.dark-mode #searchIcon {
color: #ccc; /* Dark mode icon color */
}
#searchIcon:hover {
color: #007BFF; /* Hover color */
}
/* 定位圖示樣式 */
#locationIcon {
margin-left: 10px; /* 與搜尋圖示的間距,調整為與 searchIcon 相同 */
font-size: 20px; /* 調整圖示大小,從 24px 縮小到 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;
}
.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;
display: none;
}
.overlay.show {
display: block;
}
.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;
}
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 #keyword:focus {
}
body.dark-mode #searchContainer {
background-color: #1e1e1e; /* Dark mode container background */
}
body.dark-mode #searchIcon {
color: #ccc; /* Dark mode icon color */
}
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 #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;
}
/* Styles for the combined floating menu options */
#floatingMenuOptions {
position: fixed;
bottom: 120px; /* Position above the main toggle button */
right: 20px;
display: flex;
flex-direction: column; /* Vertical alignment */
gap: 10px; /* Space between buttons */
z-index: 1000;
opacity: 0; /* Hidden by default */
visibility: hidden; /* Hidden by default */
transition: opacity 0.3s ease, visibility 0.3s ease; /* Smooth fade in/out */
}
#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);
}
/* Specific styles for dark mode toggle within the combined menu */
#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);
}
/* Main toggle button (gear icon) */
#mainToggleButton {
position: fixed;
bottom: 60px; /* Position above update countdown */
right: 20px;
z-index: 1200; /* Ensure it's on top */
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;
}
/* 針對窄螢幕的調整 */
@media (max-width: 768px) {
.station {
width: calc(50% - 20px);
}
/* Adjusted search container for horizontal layout */
#searchContainer {
padding: 8px 16px; /* 調整 padding,左右16px */
}
#keyword {
/* Adjusted width to leave space for the icon and toggle */
flex-grow: 1; /* Allow keyword to grow */
margin-right: 0;
margin-bottom: 0; /* No margin-bottom in row layout */
border-radius: 28px; /* Keep rounded corners like default */
display: block;
padding: 0 0 0 12px; /* 調整 padding-left 確保文字不會被遮蓋 */
}
#searchIcon {
position: static; /* Remain in static flow within flex container */
margin-left: 10px; /* Space between input and icon */
margin-top: 0; /* No margin-top in row layout */
align-self: auto; /* Default alignment in flex row */
font-size: 24px; /* Material Icons size */
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; /* 更小的螢幕間距,左右保持16px */
}
#keyword {
/* Adjusted width for smaller screens if needed, keeping room for icon and toggle */
flex-grow: 1; /* Allow keyword to grow */
padding: 0 0 0 12px; /* 調整 padding-left 確保文字不會被遮蓋 */
}
#searchIcon {
/* Icon styles remain consistent with 768px breakpoint */
}
}
#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="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>
<button id="mainToggleButton" title="設定" style="position: fixed; bottom: 60px; right: 20px; z-index: 1200; 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>
<button id="updateCountdown" style="display:none; position: fixed; bottom: 10px; left: 50%; transform: translateX(-50%); z-index: 1000; 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>
<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');
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);
}
});
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;
const allVehicleData = {};
const headers = {
'Accept': '*/*',
'Content-Type': 'application/json',
'Origin': 'https://www.youbike.com.tw',
'Referer': 'https://www.youbike.com.tw/'
};
for (let i = 0; i < stationIds.length; i += batchSize) {
const stationIdBatch = stationIds.slice(i, i + batchSize);
const body = JSON.stringify({ station_no: stationIdBatch });
try {
const response = await fetch('https://apis.youbike.com.tw/tw2/parkingInfo', {
method: 'POST',
headers,
body
});
const result = await response.json();
if (result.retCode === 1 && result.retVal && result.retVal.data) {
const dataArray = result.retVal.data;
dataArray.forEach(item => {
allVehicleData[item.station_no] = {
available_2_0: item.available_spaces_detail ? item.available_spaces_detail.yb2 : 0,
available_e: item.available_spaces_detail ? item.available_spaces_detail.eyb : 0,
empty_spaces: item.empty_spaces || 0
};
});
} else {
console.error("API 請求失敗:", result.retMsg);
}
} catch (e) {
console.error(e);
}
}
return allVehicleData;
}
function updateResultsOrder() {
const cards = document.querySelectorAll(".station");
cards.forEach(card => {
const pinButton = card.querySelector(".pin-button");
if (pinButton) {
pinButton.textContent = pinnedStations.has(card.dataset.id) ? '★' : '☆';
pinButton.classList.toggle('pinned', pinnedStations.has(card.dataset.id));
}
});
const sortedCards = Array.from(cards).sort((a, b) => {
const aPinned = pinnedStations.has(a.dataset.id);
const bPinned = pinnedStations.has(b.dataset.id);
if (aPinned && !bPinned) return -1;
if (!aPinned && bPinned) return 1;
const distanceA = calculateDistance(
userLocation ? userLocation.latitude : defaultLatitude,
userLocation ? userLocation.longitude : defaultLongitude,
parseFloat(a.dataset.lat),
parseFloat(a.dataset.lng)
);
const distanceB = calculateDistance(
userLocation ? userLocation.latitude : defaultLatitude,
userLocation ? userLocation.longitude : defaultLongitude,
parseFloat(b.dataset.lat),
parseFloat(b.dataset.lng)
);
return distanceA - distanceB;
});
sortedCards.forEach(card => {
resultsDiv.appendChild(card);
});
}
function showLoading() {
loadingDiv.classList.add('show');
}
function hideLoading() {
loadingDiv.classList.remove('show');
}
function showModal(message) {
modalText.textContent = message;
overlay.classList.add('show');
modal.classList.add('show');
}
function hideModal() {
overlay.classList.remove('show');
modal.classList.remove('show');
}
function calculateDistance(lat1, lon1, lat2, lon2) {
const R = 6371;
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;
}
async function handleGeolocationSuccess(position) {
const wasDefault = !userLocation || (userLocation.latitude === defaultLatitude && userLocation.longitude === defaultLongitude);
userLocation = {
latitude: position.coords.latitude,
longitude: position.coords.longitude
};
if (wasDefault) {
await searchYouBike();
updateResultsOrder();
} else {
updateCardDistances();
}
}
function handleGeolocationError(error) {
console.error("Geolocation error:", error);
locationDenied = true;
let errorMessage = "";
switch (error.code) {
case error.PERMISSION_DENIED:
errorMessage = "未提供定位權限,將使用預設座標進行定位。";
break;
case error.POSITION_UNAVAILABLE:
errorMessage = "定位資訊不可用,將使用預設座標進行定位直到定位成功。";
break;
case error.TIMEOUT:
errorMessage = "定位逾時,將使用預設座標進行定位直到定位成功。";
break;
default:
errorMessage = "定位錯誤,將使用預設座標進行定位直到定位成功。";
break;
}
showModal(errorMessage);
userLocation = { latitude: defaultLatitude, longitude: defaultLongitude };
searchYouBike();
}
function handleGeoUpdateSuccess(position) {
const wasDefault = userLocation && userLocation.latitude === defaultLatitude && userLocation.longitude === defaultLongitude;
userLocation = {
latitude: position.coords.latitude,
longitude: position.coords.longitude
};
if (wasDefault) {
searchYouBike().then(() => {
updateResultsOrder();
});
} else {
updateCardDistances();
}
}
function handleGeoUpdateError(error) {
console.error("Geolocation update error:", error);
}
function getLocation() {
if (!useLocation) {
return;
}
if (navigator.geolocation) {
let timeoutTriggered = false;
const geoTimeout = setTimeout(() => {
timeoutTriggered = true;
handleGeolocationError({ code: 3, message: "首次定位超時" });
}, 10000);
navigator.geolocation.getCurrentPosition(
function (position) {
if (!timeoutTriggered) {
clearTimeout(geoTimeout);
handleGeolocationSuccess(position);
}
},
function (error) {
if (!timeoutTriggered) {
clearTimeout(geoTimeout);
handleGeolocationError(error);
}
},
{
enableHighAccuracy: true,
timeout: 10000,
maximumAge: 0
}