-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
496 lines (471 loc) · 16.6 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Weather III (30-06-2022) - Sanajit Jana (fw19_0513)</title>
<style>
body {
font-family: sans-serif;
background: linear-gradient(
rgba(51, 170, 255, 0.8901960784313725),
#fff
);
height: 120vh;
}
#search-bar {
margin-top: 50px;
text-align: center;
}
#search-input {
padding: 10px 10px;
border: 2px solid black;
width: 300px;
font-size: 16px;
}
#search-button {
margin-left: -10px;
padding: 10px 15px;
background-color: black;
color: white;
font-size: 16px;
}
#search-button:hover {
background-color: rgb(0, 0, 0);
cursor: pointer;
}
form {
box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 8px;
width: 350px;
margin: auto;
padding: 20px 40px;
background-color: white;
}
form > p {
margin-bottom: 50px;
text-align: center;
font-size: 18px;
font-weight: 700;
}
#temp-bar {
display: flex;
justify-content: space-between;
align-items: center;
}
#temp h2 {
font-size: 28px;
font-weight: 400;
}
#temp span {
font-size: 14px;
font-weight: 400;
text-transform: capitalize;
}
#temp-img {
height: 100px;
width: 100px;
}
#location {
font-size: 28px;
font-weight: 700;
text-align: center;
margin: 40px 0px 40px 0px;
}
#weather-info-title {
font-size: 14px;
font-weight: 700;
}
#weather-info-div {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-template-rows: auto;
justify-content: center;
}
#weather-info-div > div {
width: 150px;
margin: auto;
display: flex;
justify-content: center;
}
#weather-info-div > div > img {
margin-right: 15px;
}
#container {
width: 90%;
margin: auto;
margin-top: 20px;
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-template-rows: auto;
gap: 50px;
}
#forcast {
width: 90%;
margin: auto;
margin-top: 30px;
display: flex;
flex-direction: row;
justify-content: center;
}
#forcast > div {
border-right: 1px solid #bdb7b7;
padding: 0px 25px;
text-align: center;
background-color: #28282895;
color: white;
}
#forcast > div > h2 {
font-size: 20px;
margin-top: 10px;
}
#forcast > div > img {
margin-top: -10px;
margin-bottom: -15px;
}
#forcast > div > h3:last-child {
margin-top: -10px;
font-size: 16px;
font-weight: 400;
}
#forcast > div:last-child {
border-right: 1px solid transparent;
}
.gmap_canvas {
overflow: hidden;
background: none !important;
}
#min-max {
font-size: 12px;
font-weight: 400;
margin-top: -15px;
}
</style>
</head>
<body onload="getLocation()">
<div id="search-bar">
<input type="text" id="search-input" placeholder="Enter your city name" />
<button id="search-button">Search</button>
</div>
<div id="forcast"></div>
<div id="container">
<div id="weather">
<form>
<p>Weather App</p>
<div id="temp-bar">
<div id="temp">
<h2 id="acc-teamp">26°C | <span> Overcast Clouds</span></h2>
<h4 id="min-max">(Min: 24°C | Max: 29°C)</h4>
</div>
<img
src="https://sanajitjana.github.io/react-weather-app/icons/perfect-day.svg"
id="temp-img"
alt=""
/>
</div>
<h1 id="location">Kolkata, IN</h1>
<h3 id="weather-info-title">Weather Info</h3>
<div id="weather-info-div">
<div>
<img
src="https://sanajitjana.github.io/react-weather-app/icons/temp.svg"
alt=""
/>
<p id="sunrise">
5 : 10
<br />
Sunrise
</p>
</div>
<div>
<img
src="https://sanajitjana.github.io/react-weather-app/icons/humidity.svg"
alt=""
/>
<p id="humidity">
43<br />
Humidity
</p>
</div>
<div>
<img
src="https://sanajitjana.github.io/react-weather-app/icons/wind.svg"
alt=""
/>
<p id="wind">
4.37 <br />
Wind
</p>
</div>
<div>
<img
src="https://sanajitjana.github.io/react-weather-app/icons/pressure.svg"
alt=""
/>
<p id="pressure">
1016 <br />
Pressure
</p>
</div>
</div>
</form>
</div>
<div id="map">
<iframe
id="gmap_canvas"
frameborder="0"
scrolling="no"
marginheight="0"
marginwidth="0"
width="100%"
height="100%"
></iframe>
</div>
</div>
</body>
<script>
let weatherIcon = {
sandstorm: "https://worldweather.wmo.int/images/1.png",
duststorm: "https://worldweather.wmo.int/images/1.png",
sand: "https://worldweather.wmo.int/images/1.png",
dust: "https://worldweather.wmo.int/images/1.png",
thunderstorms: "https://worldweather.wmo.int/images/2.png",
thundershowers: "https://worldweather.wmo.int/images/2.png",
storm: "https://worldweather.wmo.int/images/2.png",
lightning: "https://worldweather.wmo.int/images/2.png",
hail: "https://worldweather.wmo.int/images/3.png",
"blowing snow": "https://worldweather.wmo.int/images/4.png",
blizzard: "https://worldweather.wmo.int/images/4.png",
snowdrift: "https://worldweather.wmo.int/images/4.png",
snowstorm: "https://worldweather.wmo.int/images/4.png",
"snow showers": "https://worldweather.wmo.int/images/5.png",
flurries: "https://worldweather.wmo.int/images/5.png",
snow: "https://worldweather.wmo.int/images/6.png",
"heavy snow": "https://worldweather.wmo.int/images/6.png",
snowfall: "https://worldweather.wmo.int/images/6.png",
"light snow": "https://worldweather.wmo.int/images/7.png",
sleet: "https://worldweather.wmo.int/images/8.png",
showers: "https://worldweather.wmo.int/images/9.png",
"heavy showers": "https://worldweather.wmo.int/images/9.png",
rainshower: "https://worldweather.wmo.int/images/9.png",
"occasional showers": "https://worldweather.wmo.int/images/10.png",
"scattered showers": "https://worldweather.wmo.int/images/10.png",
"isolated showers": "https://worldweather.wmo.int/images/11.png",
"light showers": "https://worldweather.wmo.int/images/12.png",
"freezing rain": "https://worldweather.wmo.int/images/13.png",
rain: "https://worldweather.wmo.int/images/14.png",
drizzle: "https://worldweather.wmo.int/images/15.png",
"light rain": "https://worldweather.wmo.int/images/15.png",
fog: "https://worldweather.wmo.int/images/16.png",
mist: "https://worldweather.wmo.int/images/17.png",
smoke: "https://worldweather.wmo.int/images/18.png",
haze: "https://worldweather.wmo.int/images/19.png",
overcast: "https://worldweather.wmo.int/images/20.png",
"sunny intervals": "https://worldweather.wmo.int/images/21a.png",
"no rain": "https://worldweather.wmo.int/images/21a.png",
clearing: "https://worldweather.wmo.int/images/21a.png",
"sunny periods": "https://worldweather.wmo.int/images/22a.png",
"partly cloudy": "https://worldweather.wmo.int/images/22a.png",
"partly bright": "https://worldweather.wmo.int/images/22a.png",
mild: "https://worldweather.wmo.int/images/22a.png",
cloudy: "https://worldweather.wmo.int/images/23a.png",
clouds: "https://worldweather.wmo.int/images/23a.png",
"mostly cloudy": "https://worldweather.wmo.int/images/23a.png",
bright: "https://worldweather.wmo.int/images/24a.png",
sunny: "https://worldweather.wmo.int/images/24a.png",
fair: "https://worldweather.wmo.int/images/24a.png",
fine: "https://worldweather.wmo.int/images/24a.png",
clear: "https://worldweather.wmo.int/images/24a.png",
windy: "https://worldweather.wmo.int/images/26.png",
squall: "https://worldweather.wmo.int/images/26.png",
stormy: "https://worldweather.wmo.int/images/26.png",
gale: "https://worldweather.wmo.int/images/26.png",
wet: "https://worldweather.wmo.int/images/27.png",
humid: "https://worldweather.wmo.int/images/27.png",
dry: "https://worldweather.wmo.int/images/28.png",
freezing: "https://worldweather.wmo.int/images/29.png",
frost: "https://worldweather.wmo.int/images/30.png",
hot: "https://worldweather.wmo.int/images/31.png",
cold: "https://worldweather.wmo.int/images/32.png",
chilly: "https://worldweather.wmo.int/images/32.png",
warm: "https://worldweather.wmo.int/images/33.png",
cool: "https://worldweather.wmo.int/images/34.png",
"volcanic ash": "https://worldweather.wmo.int/images/35.png",
};
document
.getElementById("search-button")
.addEventListener("click", function () {
let cityname = document.getElementById("search-input").value;
fetchApiData(cityname);
fetchApiForcastData(cityname);
});
document
.getElementById("search-input")
.addEventListener("keypress", function (event) {
let cityname = document.getElementById("search-input").value;
if (event.key === "Enter") {
fetchApiData(cityname);
fetchApiForcastData(cityname);
}
});
//fetch weather data
async function fetchApiData(cityname) {
try {
const api = `https://api.openweathermap.org/data/2.5/weather?q=${cityname}&appid=6768b2e5b4a36c16f6a94477cd18cbc9`;
let res = await fetch(api);
data = await res.json();
displayWeather(data);
} catch (e) {
console.log("Error fetching weather data");
}
}
//current location
function getLocation() {
navigator.geolocation.getCurrentPosition(success);
function success(res) {
const crd = res.coords;
getWeatherOnLocation(crd.latitude, crd.longitude);
getForcastWeatherOnLocation(crd.latitude, crd.longitude);
}
}
//current location weather
async function getWeatherOnLocation(latitude, longitude) {
let url = `https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=6768b2e5b4a36c16f6a94477cd18cbc9`;
try {
let res = await fetch(url);
data = await res.json();
displayWeather(data);
} catch (e) {
console.log("Error fetching location");
}
}
function displayWeather(data) {
let mapUrl = `https://maps.google.com/maps?q=${data.name}&t=&z=13&ie=UTF8&iwloc=&output=embed`;
let temp = document.getElementById("acc-teamp");
let cloud = data.weather[0].description;
let tempCel = (data.main.temp - 273.15).toFixed(0);
temp.innerHTML = `${tempCel}°C | <span> ${cloud}</span>`;
let cDes = cloud.toLowerCase();
let cStat = data.weather[0].main.toLowerCase();
let minCel = (data.main.temp_min - 270.15).toFixed(0);
let maxCel = (data.main.temp_max - 275.15).toFixed(0);
let min_max = document.getElementById("min-max");
min_max.innerText = `(Min: ${minCel}°C | Max: ${maxCel}°C)`;
img = document.getElementById("temp-img");
if (weatherIcon[cStat]) {
img.src = weatherIcon[cStat];
} else if (weatherIcon[cDes]) {
img.src = weatherIcon[cDes];
} else {
img.src =
"https://sanajitjana.github.io/react-weather-app/icons/perfect-day.svg";
}
let ciityname = document.getElementById("location");
ciityname.innerText = `${data.name}, ${data.sys.country}`;
let isDay = data.weather[0].icon.includes("d");
let sunrise = document.getElementById("sunrise");
if (isDay) {
let val = getTime(data.sys.sunset);
sunrise.innerHTML = `${val}<br />Sunset`;
} else {
let val = getTime(data.sys.sunrise);
sunrise.innerHTML = `${val}<br />Sunrise`;
}
let humidity = document.getElementById("humidity");
humidity.innerHTML = `${data.main.humidity}<br />Humidity`;
let wind = document.getElementById("wind");
wind.innerHTML = `${data.wind.speed}<br />Wind`;
let pressure = document.getElementById("pressure");
pressure.innerHTML = `${data.main.pressure}<br />Pressure`;
let iframe = document.getElementById("gmap_canvas");
iframe.src = mapUrl;
}
//change unix time to normal timeStamp
const getTime = (timeStamp) => {
return `${new Date(timeStamp * 1000).getHours()} : ${new Date(
timeStamp * 1000
).getMinutes()}`;
};
async function fetchApiForcastData(cityname) {
try {
const api = `https://api.openweathermap.org/data/2.5/forecast?q=${cityname}&appid=6768b2e5b4a36c16f6a94477cd18cbc9`;
let res = await fetch(api);
data = await res.json();
displayForcast(data.list);
} catch (e) {
console.log("Error fetching weather data");
}
}
//current location weather
async function getForcastWeatherOnLocation(latitude, longitude) {
let url = `https://api.openweathermap.org/data/2.5/forecast?lat=${latitude}&lon=${longitude}&appid=6768b2e5b4a36c16f6a94477cd18cbc9`;
try {
let res = await fetch(url);
data = await res.json();
filterForecast5days(data.list);
} catch (e) {
console.log("Error fetching location");
}
}
function filterForecast5days(data) {
let ans = data.filter(function (weather) {
let date = weather.dt_txt.split(" ");
if (date[1] == "18:00:00") {
return weather;
}
});
displayForcast(ans);
}
let newDay = new Date();
let cDay = newDay.getDay();
let i = 0;
function displayForcast(data) {
let forcast = document.getElementById("forcast");
forcast.innerHTML = "";
let index = 0;
data.forEach(function (weather) {
if (index > 6) {
return 0;
}
index++;
if (!weather) {
return 0;
}
let div = document.createElement("div");
let week = document.createElement("h2");
if (cDay == 7) {
cDay = 0;
i = 0;
}
week.innerText = findDay(cDay);
cDay++;
i++;
let cStat = weather.weather[0].main.toLowerCase();
let img = document.createElement("img");
if (weatherIcon[cStat]) {
img.src = weatherIcon[cStat];
} else {
img.src =
"https://sanajitjana.github.io/react-weather-app/icons/perfect-day.svg";
}
let maxCel = (weather.main.temp_max - 270.15).toFixed(0);
let maxTemp = document.createElement("h3");
maxTemp.innerText = `${maxCel}°`;
let minCel = (weather.main.temp_min - 275.15).toFixed(0);
let minTemp = document.createElement("h3");
minTemp.innerText = `${minCel}°`;
div.append(week, img, maxTemp, minTemp);
forcast.append(div);
});
}
//find week-day
function findDay(num) {
var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
for (let i = 0; i < days.length; i++) {
if (i == num) {
return days[i];
}
}
}
</script>
</html>