Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a1c797c

Browse files
authored
Merge pull request tajulafreen#10 from tajulafreen/weather-app
50Projects-HTML-CSS-JavaScript : Weather app
2 parents 931cb9b + 18220e8 commit a1c797c

File tree

11 files changed

+414
-0
lines changed

11 files changed

+414
-0
lines changed
1.86 MB
Loading
1.93 MB
Loading
1.84 MB
Loading
3.66 MB
Loading
1.39 MB
Loading
622 KB
Loading
524 KB
Loading
1.82 MB
Loading

Source-Code/WeatherApp/index.html

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Weather App</title>
7+
<link rel="stylesheet" href="style.css">
8+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
9+
</head>
10+
<body>
11+
<div class="weather-app night-clear">
12+
<div class="weather-container">
13+
14+
<ul class="weather-information">
15+
<!-- <h2 class="heading">Check Weather</h2>
16+
-->
17+
<h1 id="temp" class="temp">23°C</h1>
18+
<li class="column">
19+
<h1 id="city-name" class="city">Kadapa</h1>
20+
<h4 id="date-time" class="time">2024-02-06 3:59</h4>
21+
</li>
22+
23+
<img id="icon" class="icon" class="icon" alt="icon" />
24+
25+
26+
</ul>
27+
<ul class="weather-suggestions">
28+
29+
<input class="input" id="input" type="text" placeholder="Enter city name">
30+
<button class="search" id="btn"><i class="fa fa-search"></i></button>
31+
32+
<ul>
33+
<li>
34+
<a class="location" id="getlocation"><i class="fa fa-location-arrow "> Get Weather of your city</i></a>
35+
</li>
36+
<li class="city">London</li>
37+
<li class="city">Kabul</li>
38+
<li class="city">Ottawa</li>
39+
<li class="city">california</li>
40+
</ul>
41+
<h3 class="weather-de">Weather details</h3>
42+
<li class="weather-details">
43+
44+
<div class="row">
45+
<p class="country">Country</p>
46+
<span id="country" class="country">India</span>
47+
48+
</div>
49+
<div class="row">
50+
<p class="condition">Condition</p>
51+
<span id="condition2" class="condition">Clear</span>
52+
53+
</div>
54+
<div class="row">
55+
<p class="humidity">Humidity</p>
56+
<span id="humidity" class="humidity">humidity</span>
57+
58+
</div>
59+
</li>
60+
</ul>
61+
</div>
62+
63+
64+
<script src="script.js"></script>
65+
</div>
66+
</body>
67+
</html>

Source-Code/WeatherApp/script.js

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
const input = document.getElementById('input');
2+
const btn = document.getElementById('btn');
3+
const apiKey = 'e3a46268fdc2475cb63214712240202';
4+
const cityName = document.getElementById('city-name');
5+
const dateTime = document.getElementById('date-time');
6+
const condition2 = document.getElementById('condition2');
7+
const temp = document.getElementById('temp');
8+
const humidity = document.getElementById('humidity');
9+
const country = document.getElementById('country');
10+
const locat = document.getElementById('getlocation');
11+
const cities = document.getElementsByClassName('city');
12+
const icon = document.getElementById('icon');
13+
const body = document.querySelector('.weather-app');
14+
const fetchData = async (url) => {
15+
try {
16+
const data = await fetch(url);
17+
if (!data.ok) {
18+
throw new Error(data.statusText);
19+
}
20+
return data.json();
21+
} catch (error) {
22+
console.log(error);
23+
throw error;
24+
}
25+
};
26+
27+
const updateWeatherInfo = (result) => {
28+
const { error, location, current } = result;
29+
if (error) {
30+
cityName.innerText = `Error: ${error.message}`;
31+
[country, dateTime, temp, humidity, condition2, icon].forEach((elem) => {
32+
elem.innerText = '';
33+
});
34+
icon.src = '';
35+
} else {
36+
const { name, country, localtime } = location;
37+
cityName.innerText = name;
38+
country.innerText = country;
39+
dateTime.innerText = localtime;
40+
temp.innerText = `${current.temp_c} °C`;
41+
humidity.innerText = `${current.humidity} %`;
42+
condition2.innerText = current.condition.text;
43+
icon.src = current.condition.icon;
44+
45+
const isDay = current.is_day === 1 ? 'day' : 'night';
46+
const codes = [
47+
[1000, 10000, 10001, 1100, 11001, 11000, 51190, 60030], // clear
48+
[
49+
1101, 11011, 11010, 11021, 11020, 1102, 1001, 10010, 10011, 1003, 1150,
50+
1153, 1168, 1147, 1135, 1087, 1003, 1006, 1207, 1009,
51+
], // cloudy
52+
[
53+
1261, 1273, 1276, 1274, 1246, 1243, 1240, 1201, 1204, 1198, 1192, 1195,
54+
1189, 1186, 1183, 1180, 1186,
55+
], // rainy
56+
[
57+
1030, 1066, 1168, 1171, 1210, 1213, 1216, 1219, 1222, 1225, 1237, 1255,
58+
1258, 1279, 1282,
59+
], // snowy
60+
];
61+
const imageUrls = [
62+
`./images/${isDay}/clear.jpg`,
63+
`./images/${isDay}/cloudy.jpg`,
64+
`./images/${isDay}/rainy.jpg`,
65+
`./images/${isDay}/snowy.jpg`,
66+
];
67+
68+
for (let i = 0; i < codes.length; i += 1) {
69+
if (codes[i].includes(current.condition.code)) {
70+
body.style.backgroundImage = `url('${imageUrls[i]}')`;
71+
console.log(imageUrls[i]);
72+
break;
73+
}
74+
}
75+
}
76+
};
77+
const getData = async (cityName) => {
78+
try {
79+
const result = await fetchData(
80+
`http://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${cityName}&aqi=no`,
81+
);
82+
return result;
83+
} catch (error) {
84+
return {
85+
error: { message: 'Failed to fetch data. Please try again later.' },
86+
};
87+
}
88+
};
89+
const getlocation = async (lat, long) => fetchData(
90+
`http://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${lat},${long}&aqi=no`,
91+
);
92+
93+
const gotlocation = async (position) => {
94+
try {
95+
const result = await getlocation(
96+
position.coords.latitude,
97+
position.coords.longitude,
98+
);
99+
console.log(result);
100+
updateWeatherInfo(result);
101+
} catch (error) {
102+
cityName.innerText = 'Error fetching weather based on location';
103+
}
104+
};
105+
const failedlocation = () => console.log('failed to locate location');
106+
107+
btn.addEventListener('click', async (e) => {
108+
try {
109+
if (input.value.length === 0) {
110+
alert('Please type a city name');
111+
} else {
112+
const { value } = input;
113+
const result = await getData(value);
114+
console.log(result);
115+
updateWeatherInfo(result);
116+
console.log(result);
117+
}
118+
} catch (error) {
119+
cityName.innerText = 'Error to fetch weather';
120+
}
121+
e.preventDefault();
122+
});
123+
124+
locat.addEventListener('click', () => navigator.geolocation.getCurrentPosition(gotlocation, failedlocation));
125+
const citiesArray = [...cities];
126+
citiesArray.forEach((element) => {
127+
element.addEventListener('click', async () => {
128+
const cityName = element.innerText;
129+
const result = await getData(cityName);
130+
updateWeatherInfo(result);
131+
});
132+
});
133+
134+
window.addEventListener('load', async () => {
135+
navigator.geolocation.getCurrentPosition(gotlocation, failedlocation);
136+
});

0 commit comments

Comments
 (0)