-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
222 lines (204 loc) · 8.35 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
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>실시간 날씨 정보</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
color: #333;
text-align: center;
}
.container {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 20px;
margin: 20px;
}
.weather-card, .hourly-weather-card {
background-color: #fff;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
padding: 20px;
max-width: 250px;
text-align: left;
}
.weather-card h2, .hourly-weather-card h2 {
margin-top: 0;
}
.weather-card p, .hourly-weather-card p {
margin: 5px 0;
display: flex;
align-items: center;
}
.weather-card p span, .hourly-weather-card p span {
margin-right: 8px;
}
.weather-icon {
font-size: 24px;
}
.hourly-container {
display: flex;
overflow-x: auto;
gap: 10px;
padding: 20px 0;
white-space: nowrap;
margin: 0 20px;
border-radius: 10px;
background-color: #fff;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}
.hourly-weather-card {
min-width: 150px;
flex: none;
}
.scroll-button {
background-color: #007bff;
border: none;
color: white;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 10px;
cursor: pointer;
border-radius: 5px;
}
select {
margin: 20px;
padding: 10px;
font-size: 16px;
}
@media (max-width: 600px) {
.weather-card, .hourly-weather-card {
max-width: 100%;
padding: 10px;
}
.weather-card h2, .hourly-weather-card h2 {
font-size: 1.2em;
}
.weather-icon {
font-size: 18px;
}
}
</style>
</head>
<body>
<h1>실시간 날씨 정보</h1>
<select id="location-select" onchange="displayWeather()">
<option value="0">도쿄 하네다 공항</option>
<option value="1">요코하마</option>
<option value="2">가마쿠라고고마에역</option>
<option value="3">후지산 가와구치호</option>
<option value="4">오와쿠다니 계곡</option>
<option value="5" selected>도쿄 긴자</option>
<option value="6">우에노</option>
<option value="7">롯폰기</option>
<option value="8">시부야</option>
<option value="9">신주쿠</option>
<option value="10">이케부쿠로</option>
</select>
<div class="container" id="weather-container">
<!-- 날씨 카드가 여기에 추가됩니다 -->
</div>
<div>
<button class="scroll-button" onclick="scrollHourly(-1)">◀</button>
<button class="scroll-button" onclick="scrollHourly(1)">▶</button>
</div>
<div class="hourly-container" id="hourly-container">
<!-- 시간별 날씨 카드가 여기에 추가됩니다 -->
</div>
<script>
const locations = [
{name: "도쿄 하네다 공항", lat: 35.5494, lon: 139.7798},
{name: "요코하마", lat: 35.4437, lon: 139.6380},
{name: "가마쿠라고고마에역", lat: 35.3183, lon: 139.5509},
{name: "후지산 가와구치호", lat: 35.4975, lon: 138.7551},
{name: "오와쿠다니 계곡", lat: 35.2323, lon: 139.0274},
{name: "도쿄 긴자", lat: 35.6717, lon: 139.7650},
{name: "우에노", lat: 35.7138, lon: 139.7745},
{name: "롯폰기", lat: 35.6604, lon: 139.7292},
{name: "시부야", lat: 35.6580, lon: 139.7016},
{name: "신주쿠", lat: 35.6938, lon: 139.7036},
{name: "이케부쿠로", lat: 35.7295, lon: 139.7109}
];
const apiKey = '023642dbc95680d29d406f62dda62bbd';
function getWeatherIcon(description) {
if (description.includes('비')) {
return '🌧️';
} else if (description.includes('구름많음')) {
return '☁️';
} else if (description.includes('구름조금')) {
return '🌥️';
} else {
return '☀️';
}
}
async function fetchWeather(lat, lon) {
try {
const response = await fetch(`https://api.openweathermap.org/data/2.5/forecast?lat=${lat}&lon=${lon}&appid=${apiKey}&units=metric&lang=kr`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('Error fetching weather data:', error);
return null;
}
}
async function displayWeather() {
const container = document.getElementById('weather-container');
const hourlyContainer = document.getElementById('hourly-container');
container.innerHTML = '';
hourlyContainer.innerHTML = '';
const select = document.getElementById('location-select');
const location = locations[select.value];
const weatherData = await fetchWeather(location.lat, location.lon);
if (weatherData) {
// 3일치 예보
for (let i = 0; i < 3; i++) {
const card = document.createElement('div');
card.className = 'weather-card';
const forecast = weatherData.list[i * 8]; // 3일간의 날씨 (8시간 간격)
const icon = getWeatherIcon(forecast.weather[0].description);
card.innerHTML = `
<h2>${location.name} (${new Date(forecast.dt_txt).toLocaleDateString()})</h2>
<p><span class="weather-icon">${icon}</span>온도: ${forecast.main.temp}°C</p>
<p><span class="weather-icon">${icon}</span>날씨: ${forecast.weather[0].description}</p>
<p><span class="weather-icon">💧</span>습도: ${forecast.main.humidity}%</p>
`;
container.appendChild(card);
}
// 시간별 예보 (24시간)
for (let i = 0; i < 24; i++) {
const card = document.createElement('div');
card.className = 'hourly-weather-card';
const forecast = weatherData.list[i];
const icon = getWeatherIcon(forecast.weather[0].description);
card.innerHTML = `
<h2>${new Date(forecast.dt_txt).toLocaleTimeString([], {hour: '2-digit', minute: '2-digit'})}</h2>
<p>${new Date(forecast.dt_txt).toLocaleDateString()}</p>
<p><span class="weather-icon">${icon}</span>온도: ${forecast.main.temp}°C</p>
<p><span class="weather-icon">${icon}</span>날씨: ${forecast.weather[0].description}</p>
<p><span class="weather-icon">💧</span>습도: ${forecast.main.humidity}%</p>
`;
hourlyContainer.appendChild(card);
}
}
}
function scrollHourly(direction) {
const container = document.getElementById('hourly-container');
const scrollAmount = 150; // 한번에 스크롤할 양
container.scrollBy({
left: direction * scrollAmount,
behavior: 'smooth'
});
}
displayWeather();
</script>
</body>
</html>