-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.js
42 lines (37 loc) · 1.67 KB
/
weather.js
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
window.addEventListener('load', () => {
async function fetchWeather(latitude, longitude) {
try {
let apiUrl = '';
if (latitude && longitude) {
apiUrl = `https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=8222405af0a81007c0f6e25d5aced1ae`;
} else {
apiUrl = 'https://api.openweathermap.org/data/2.5/weather?q=Delhi&appid=8222405af0a81007c0f6e25d5aced1ae';
}
const response = await fetch(apiUrl);
const data = await response.json();
const temperature = Math.round(data.main.temp - 273.15);
const icon = data.weather[0].icon;
const location = data.name;
document.querySelector('.temperature').textContent = `${temperature}°C`;
document.querySelector('.location').textContent = location;
document.querySelector('.weather-icon').style.backgroundImage = `url('https://openweathermap.org/img/wn/${icon}.png')`;
} catch (error) {
console.error('Error fetching weather data:', error);
}
}
function success(position) {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
fetchWeather(latitude, longitude);
}
function error() {
console.error('Unable to retrieve location');
fetchWeather();
}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error);
} else {
console.error('Geolocation is not supported by this browser');
fetchWeather();
}
});