-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
49 lines (46 loc) · 2.21 KB
/
app.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
43
44
45
46
47
48
49
'use strict';
const loader = document.querySelector('.loader');
const getLocationWeather = async () => {
try{
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(
async pos => {
const lat = pos.coords.latitude;
const lon = pos.coords.longitude;
const API_KEY = '692dc59d5191d0c6036a4c3712840bc3';
const weatherIconURL = 'https://openweathermap.org/img/wn/';
const baseURL = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${API_KEY}&units=metric&lang=es`;
const data = await fetch(baseURL);
const weatherData = await data.json();
//console.log(weatherData);
console.log(baseURL);
document.querySelector('.temp-icon').setAttribute('src', weatherIconURL + weatherData.weather[0].icon + '@2x.png');
document.querySelector('.weather-temp').textContent = Math.round(weatherData.main.temp) + 'ºC';
document.querySelector('.weather-description').textContent = weatherData.weather[0].description;
document.querySelector('.city').textContent = weatherData.name;
document.querySelector('.country').textContent = weatherData.sys.country;
document.querySelector('.min-temp').textContent = Math.round(weatherData.main.temp_min) + 'ºC';
document.querySelector('.feels').textContent = Math.round(parseFloat(weatherData.main.feels_like)) + 'ºC';
document.querySelector('.max-temp').textContent = Math.round(weatherData.main.temp_max) + 'ºC';
setTimeout(() => {
loader.classList.toggle('loader--hidden');
}, 1500);
},
err => {
throw(err);
},
{
enableHighAccuracy: true,
timeout: 10000
}
);
}
else{
throw('Geolocation no supported');
}
}
catch(err){
console.log(err);
}
};
getLocationWeather();