-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
45 lines (37 loc) · 1.29 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
const API_KEY = '1f00855b83c4f5a37a2f6f053475ad91'
const fetchData = position => {
const {latitude, longitude} = position.coords
// Hace una solicitud para obtener los datos
fetch(`http://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&units=metric&appid=${API_KEY}`)
// Cuando hay respuesta:
.then(response => response.json())
.then(data => setWeatherData(data))
}
const setWeatherData = data => {
console.log(data)
const weatherData = {
location: data.name,
description: data.weather[0].main,
humidity: data.main.humidity,
pressure: data.main.pressure,
temperature: data.main.temp + '°',
date: getDate()
}
Object.keys(weatherData).forEach( key => {
document.getElementById(key).textContent = weatherData[key]
cleanUp()
})
}
const cleanUp = () => {
let container = document.getElementById('container')
let loader = document.getElementById('loader')
loader.style.display = 'none'
container.style.display = 'flex'
}
const getDate = () => {
let date = new Date()
return `${date.getDate()}-${( '0' + (date.getMonth() + 1)).slice(-2)}-${date.getFullYear()}`
}
const onLoad = () => {
navigator.geolocation.getCurrentPosition(fetchData)
}