-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
323 lines (282 loc) · 11.4 KB
/
index.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
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>氣象資訊顯示 WRV</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/js/all.min.js"></script>
<style>
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+TC:wght@400;500;700&display=swap');
:root {
--primary-color: #2196F3;
--secondary-color: #03A9F4;
--background-color: #f5f7fa;
--card-background: #ffffff;
--text-color: #333333;
--transition-speed: 0.3s;
}
body {
font-family: 'Noto Sans TC', sans-serif;
margin: 0;
padding: 20px;
background-color: var(--background-color);
color: var(--text-color);
transition: all var(--transition-speed);
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
h1 {
font-size: 2.5rem;
text-align: center;
margin-bottom: 30px;
color: var(--primary-color);
animation: fadeInDown 1s;
}
.controls {
display: flex;
gap: 15px;
margin-bottom: 30px;
justify-content: center;
animation: fadeIn 1s 0.3s both;
}
select {
padding: 12px 20px;
font-size: 16px;
border: 2px solid var(--primary-color);
border-radius: 8px;
background-color: var(--card-background);
color: var(--text-color);
cursor: pointer;
transition: all var(--transition-speed);
outline: none;
min-width: 150px;
}
select:hover {
border-color: var(--secondary-color);
box-shadow: 0 0 10px rgba(33, 150, 243, 0.3);
}
.weather-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
gap: 20px;
animation: fadeIn 1s 0.6s both;
}
.weather-card {
background-color: var(--card-background);
border-radius: 12px;
padding: 20px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
transition: all var(--transition-speed);
opacity: 0;
animation: fadeInUp 0.6s forwards;
}
.weather-card:hover {
transform: translateY(-5px);
box-shadow: 0 8px 15px rgba(0, 0, 0, 0.2);
}
.weather-icon {
font-size: 2rem;
margin-bottom: 15px;
color: var(--primary-color);
}
.weather-title {
font-size: 1.2rem;
font-weight: 500;
margin-bottom: 10px;
color: var(--primary-color);
}
.weather-value {
font-size: 1.8rem;
font-weight: 700;
margin-bottom: 5px;
}
.weather-unit {
font-size: 1rem;
color: #666;
}
.credits {
text-align: center;
margin-top: 30px;
font-size: 0.9rem;
color: #666;
animation: fadeIn 1s 0.9s both;
}
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.loading {
display: flex;
justify-content: center;
align-items: center;
height: 200px;
}
.loading-spinner {
border: 4px solid #f3f3f3;
border-top: 4px solid var(--primary-color);
border-radius: 50%;
width: 40px;
height: 40px;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
</head>
<body>
<div class="container">
<h1 class="animate__animated animate__fadeInDown">即時氣象資訊 WRV</h1>
<div class="controls animate__animated animate__fadeIn">
<select id="countySelect">
<option value="">選擇市區</option>
</select>
<select id="townSelect">
<option value="">選擇鄉鎮</option>
</select>
</div>
<div id="weatherInfo" class="weather-grid">
<div class="loading">
<div class="loading-spinner"></div>
</div>
</div>
<div class="credits animate__animated animate__fadeIn">
由 RealTech Ltd. 進行開發,資料來源:中央氣象署 CWA
</div>
</div>
<script>
const apiUrl = 'https://opendata.cwa.gov.tw/api/v1/rest/datastore/O-A0001-001?Authorization=CWA-19B695DB-613E-496B-A962-8A9399FD36A3&format=JSON';
let allStations = [];
let counties = [];
let towns = {};
let selectedCounty = '';
let selectedTown = '';
function fetchWeatherData() {
document.getElementById('weatherInfo').innerHTML = '<div class="loading"><div class="loading-spinner"></div></div>';
fetch(apiUrl)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok ' + response.statusText);
}
return response.json();
})
.then(data => {
allStations = data.records.Station || [];
populateSelectors();
updateSelectors();
filterStations();
})
.catch(error => {
document.getElementById('weatherInfo').innerHTML = `
<div class="weather-card" style="grid-column: 1 / -1; text-align: center;">
<div class="weather-icon"><i class="fas fa-exclamation-triangle"></i></div>
<div class="weather-title">錯誤</div>
<div class="weather-value">${error.message}</div>
</div>
`;
});
}
function populateSelectors() {
const countySelect = document.getElementById('countySelect');
const townSelect = document.getElementById('townSelect');
counties = [...new Set(allStations.map(station => station.GeoInfo?.CountyName))].filter(Boolean);
towns = allStations.reduce((acc, station) => {
const county = station.GeoInfo?.CountyName;
const town = station.GeoInfo?.TownName;
if (county && town) {
if (!acc[county]) {
acc[county] = new Set();
}
acc[county].add(town);
}
return acc;
}, {});
countySelect.innerHTML = '<option value="">選擇市區</option>' +
counties.map(county => `<option value="${county}">${county}</option>`).join('');
countySelect.addEventListener('change', updateTowns);
townSelect.addEventListener('change', filterStations);
}
function updateTowns() {
const county = document.getElementById('countySelect').value;
selectedCounty = county;
const townSelect = document.getElementById('townSelect');
townSelect.innerHTML = '<option value="">選擇鄉鎮</option>' +
(towns[county] ? [...towns[county]].map(town => `<option value="${town}">${town}</option>`).join('') : '');
filterStations();
}
function updateSelectors() {
const countySelect = document.getElementById('countySelect');
const townSelect = document.getElementById('townSelect');
countySelect.value = selectedCounty;
updateTowns();
townSelect.value = selectedTown;
}
function filterStations() {
const county = document.getElementById('countySelect').value;
const town = document.getElementById('townSelect').value;
selectedCounty = county;
selectedTown = town;
const filteredStations = allStations.filter(station => {
return (!county || station.GeoInfo?.CountyName === county) &&
(!town || station.GeoInfo?.TownName === town);
});
displayWeatherData(filteredStations);
}
function getWeatherIcon(weather) {
if (!weather) return 'fa-question';
if (weather.includes('晴')) return 'fa-sun';
if (weather.includes('雨')) return 'fa-cloud-rain';
if (weather.includes('陰')) return 'fa-cloud';
if (weather.includes('雲')) return 'fa-cloud-sun';
return 'fa-cloud';
}
function displayWeatherData(stations) {
if (!stations || stations.length === 0) {
document.getElementById('weatherInfo').innerHTML = `
<div class="weather-card" style="grid-column: 1 / -1; text-align: center;">
<div class="weather-icon"><i class="fas fa-search"></i></div>
<div class="weather-title">無資料</div>
<div class="weather-value">該地區無可顯示的氣象站資料</div>
</div>
`;
return;
}
let htmlContent = '';
stations.forEach((station, index) => {
const weather = station.WeatherElement?.Weather || '未知';
const temp = station.WeatherElement?.AirTemperature || '未知';
const humidity = station.WeatherElement?.RelativeHumidity || '未知';
const windSpeed = station.WeatherElement?.WindSpeed || '未知';
htmlContent += `
<div class="weather-card" style="animation-delay: ${index * 0.1}s">
<div class="weather-icon"><i class="fas ${getWeatherIcon(weather)}"></i></div>
<div class="weather-title">${station.StationName || '未知站點'}</div>
<div class="weather-value">${weather}</div>
<hr>
<div class="weather-title"><i class="fas fa-thermometer-half"></i> 溫度</div>
<div class="weather-value">${temp}<span class="weather-unit">°C</span></div>
<div class="weather-title"><i class="fas fa-tint"></i> 濕度</div>
<div class="weather-value">${humidity}<span class="weather-unit">%</span></div>
<div class="weather-title"><i class="fas fa-wind"></i> 風速</div>
<div class="weather-value">${windSpeed}<span class="weather-unit">m/s</span></div>
</div>
`;
});
document.getElementById('weatherInfo').innerHTML = htmlContent;
}
setInterval(fetchWeatherData, 35000);
fetchWeatherData();
</script>
</body>
</html>