-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPI Weather Application
258 lines (217 loc) · 7.44 KB
/
API Weather Application
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
# Weather Application
## Overview
Dive into my API Weather Web Application project, a testament to the power of integrating real-time weather data using open APIs. Developed in Visual Studio Code and guided by a YouTube tutorial, this project showcases the journey from HTML structure to dynamic JavaScript functionality. A pivotal aspect of this endeavor involved tapping into live weather data and forecasts through the OpenWeatherMap API, enriching the user experience with up-to-date weather information.
---
## Learning Journey
Beginning with HTML, I laid the groundwork for the web application's structure, and CSS then weaved its magic to craft a visually appealing design. However, the heart of the project lies in JavaScript, where I seamlessly integrated the OpenWeatherMap API to pull and display live weather data. This process not only added functionality but also provided users with accurate and real-time weather updates.
---
## Importance of API Integration
The essence of this project lies in the integration of external APIs. By signing up for a free account on OpenWeatherMap, I obtained a unique API key. This key became the gateway to accessing live weather data and forecasts. The integration process not only required technical finesse but also emphasized the significance of leveraging external resources to enhance web applications. Through this experience, I learned the importance of API keys in facilitating secure and authorized access to external data.
---
## Features
- **HTML Structure:** Crafted the foundation of the weather application, ensuring a user-friendly layout.
- **CSS Styling:** Enhanced the visual appeal with a responsive and visually appealing design.
- **JavaScript Interaction:** Seamlessly integrated the OpenWeatherMap API to fetch and display live weather information.
- **API Key Authorization:** Obtained a free API key, underscoring the importance of secure access to external data.
---
## Results
This project not only serves as a showcase of web development skills but also highlights the pivotal role of API integration. By obtaining and using an API key, I successfully brought live weather data to the fingertips of users, enriching the functionality and relevance of the web application.
_______________________________________________________________________________
### HTML
index.html
<p>Hello! Welcome to my Weather API Project!</p>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weather API Project</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="card">
<div class="search">
<input type=""text" placeholder="enter city name"
spellcheck="false">
<button><img src="weather-img/images/search.png"></button>
</div>
<div class="error">
<p>Invalid city name</p>
</div>
<div class="weather">
<img src="weather-img/images/rain.png" class="weather-icon">
<h1 class="temp">22°C</h1>
<h2 class="city">New York</h2>
<div class="details">
<div class="col">
<img src="weather-img/images/humidity.png">
<div>
<p class="humidity">50%</p>
<p>Humidity</p>
</div>
</div>
<div class="col">
<img src="weather-img/images/wind.png">
<div>
<p class="wind">15 km/h</p>
<p>Wind Speed</p>
</div>
</div>
</div>
</div>
<script>
const apiKey = "5eb3e513cbac8916c8edc015c1e4a69b";
const apiUrl = "https://api.openweathermap.org/data/2.5/weather?units=metric&q=";
const searchBox = document.querySelector(".search input");
const searchBtn = document.querySelector(".search button");
const weatherIcon = document.querySelector(".weather-icon");
async function checkWeather(city){
const response = await fetch(apiUrl + city + `&appid=${apiKey}`);
if(response.status == 404){
document.querySelector(".error").style.display = "block";
document.querySelector(".weather").style.display = "none";
}else{
var data = await response.json();
document.querySelector(".city").innerHTML = data.name;
document.querySelector(".temp").innerHTML = Math.round(data.main.temp) + "°C";
document.querySelector(".humidity").innerHTML = data.main.humidity + "%";
document.querySelector(".wind").innerHTML = data.wind.speed + " km/h";
if(data.weather[0].main == "Clouds") {
weatherIcon.src = "weather-img/images/clouds.png";
}
else if(data.weather[0].main == "Clear"){
weatherIcon.src = "weather-img/images/clear.png";
}
else if(data.weather[0].main == "Rain"){
weatherIcon.src = "weather-img/images/rain.png";
}
else if(data.weather[0].main == "Drizzle"){
weatherIcon.src = "weather-img/images/drizzle.png";
}
else if(data.weather[0].main == "Mist"){
weatherIcon.src = "weather-img/images/mist.png";
}
document.querySelector(".weather").style.display = "block";
document.querySelector(".error").style.display = "none";
}
var data = await response.json();
document.querySelector(".city").innerHTML = data.name;
document.querySelector(".temp").innerHTML = Math.round(data.main.temp) + "°C";
document.querySelector(".humidity").innerHTML = data.main.humidity + "%";
document.querySelector(".wind").innerHTML = data.wind.speed + " km/h";
if(data.weather[0].main == "Clouds") {
weatherIcon.src = "weather-img/images/clouds.png";
}
else if(data.weather[0].main == "Clear"){
weatherIcon.src = "weather-img/images/clear.png";
}
else if(data.weather[0].main == "Rain"){
weatherIcon.src = "weather-img/images/rain.png";
}
else if(data.weather[0].main == "Drizzle"){
weatherIcon.src = "weather-img/images/drizzle.png";
}
else if(data.weather[0].main == "Mist"){
weatherIcon.src = "weather-img/images/mist.png";
}
document.querySelector(".weather").style.display = "block";
}
searchBtn.addEventListener("click", ()=>{
checkWeather(searchBox.value);
})
</script>
</body>
</html>
__________________________________________________________________________________
### CSS
*{
margin: 0;
padding: 0;
font-family: 'Poppins', sans-serif;
box-sizing: border-box;
}
body{
background: #202
}
.card{
width: 90%;
max-width: 470px;
background: linear-gradient(123deg, #00feba, #5b548a);
color:#fff;
margin: 20px auto 0;
border-radius: 20px;
padding: 40px 35px;
text-align: center;
}
.search{
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
}
.search input{
border: 0;
outline: 0;
background: #ebfffc;
color: #555;
padding: 10px 25px;
height: 60px;
border-radius: 30px;
flex: 1;
margin-right: 16px;
font-size: 18px;
}
.search button{
border: 0;
outline: 0;
background: #ebfffc;
border-radius: 50%;
width: 60px;
height: 60px;
cursor: pointer;
}
.search button img{
width: 16px;
}
.weather-icon{
width: 170px;
margin-top: 30px;
}
.weather h1{
font-size: 80px;
font-weight: 500;
}
.weather h2{
font-size: 45px;
font-weight: 400;
margin-top: -10px;
}
.details{
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 20px;
margin-top: 50px;
}
.col{
display: flex;
align-items: center;
text-align: left;
}
.col img{
width: 40px;
margin-right: 10px;
}
.humidity, .wind{
font-size: 28px;
margin-top: -6px
}
.weather{
display: none;
}
.error{
text-align: left;
margin-left: 10px;
font-size: 14px;
margin-top: 10px;
display: none;
}