-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather.html
More file actions
136 lines (127 loc) · 4.87 KB
/
weather.html
File metadata and controls
136 lines (127 loc) · 4.87 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weather App</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-image:url("gorgeous-clouds-background-with-blue-sky-design_1017-25501.avif") ;
background-repeat: no-repeat;
background-size: cover;
}
.weather-container {
text-align: center;
background-color:transparent;
backdrop-filter: blur(20px);
border-radius: 10px;
box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.1);
padding: 20px;
max-width: 800px;
height: 600px; ;
width: 90%;
}
h2 {
margin-top: 0;
color: #333;
}
label {
display: block;
margin-bottom: 10px;
color: #666;
}
input[type="text"] {
width: calc(100% - 22px);
padding: 10px;
border-radius: 5px;
border: 1px solid #ddd;
}
button {
margin-top: 50px;
background-color: grey;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
height: 50px;
width: 200px;
}
button:hover {
background-color: #45a049;
}
#weather-info {
margin-top: 20px;
text-align: left;
color: white;
}
#weather-info p {
margin-bottom: 10px;
}
.error-message {
color: red;
}
</style>
</head>
<body>
<div class="weather-container">
<h2 style="color: white;margin-top: 100px;font-size: 70px;">Weather App</h2>
<label for="location" style="color: white;">Enter location:</label>
<input type="text" id="location" style="width: 70%;" placeholder="City, Country">
<br>
<button onclick="getWeather()">Get Weather</button>
<div id="weather-info"></div>
</div>
<script>
async function getWeather() {
const locationInput = document.getElementById('location').value;
const apiKey = 'd83b17c0bf494fe6b0e150555242602'; // Replace with your WeatherAPI.com API key
let apiUrl = `https://api.weatherapi.com/v1/current.json?key=${apiKey}&aqi=no`;
if (locationInput) {
apiUrl += `&q=${encodeURIComponent(locationInput)}`;
} else {
if ('geolocation' in navigator) {
navigator.geolocation.getCurrentPosition(position => {
const lat = position.coords.latitude;
const lon = position.coords.longitude;
apiUrl += `&q=${lat},${lon}`;
fetchWeather(apiUrl);
}, error => {
console.error(error);
document.getElementById('weather-info').innerHTML = 'Error: Unable to retrieve your location.';
});
} else {
document.getElementById('weather-info').innerHTML = 'Geolocation is not supported by your browser.';
}
}
fetchWeather(apiUrl);
}
async function fetchWeather(apiUrl) {
try {
const response = await fetch(apiUrl);
const data = await response.json();
if (data.error) {
document.getElementById('weather-info').innerHTML = `<center><p class="error-message">Error: ${data.error.message}</p></center>`;
} else {
const weatherInfo = `
<center><h3 style="font-size: 25px;">${data.location.name}, ${data.location.country}</h3></center>
<center><p style="font-size: 25px;">Temperature: ${data.current.temp_c}°C</p></center>
<center><p style="font-size: 25px;">Condition: ${data.current.condition.text}</p></center>
`;
document.getElementById('weather-info').innerHTML = weatherInfo;
}
} catch (error) {
console.error('Error fetching weather data:', error);
document.getElementById('weather-info').innerHTML = '<center><p class="error-message">Error: Unable to fetch weather data.</p></center>';
}
}
</script>
</body>
</html>