-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
67 lines (53 loc) · 2.41 KB
/
main.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
window.addEventListener("load", (result) => {
setTimeout(() => {
document.querySelector(".container1").style.display = "none";
document.querySelector(".container2").classList.add("container2-show")
}, 2000)
let long;
let lat;
let temperatureDescription = document.querySelector('.temperature-description');
let temperatureDegree = document.querySelector('.temperature-degree');
let locationTimezone = document.querySelector('.location-timezone');
let temperatureSection = document.querySelector('.temperature-section')
let temperatureSpan = document.querySelector('.degree-type')
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(pos => {
long = pos.coords.longitude;
lat = pos.coords.latitude;
// Solution from stack overflow
const proxyUrl = 'https://cors-anywhere.herokuapp.com/';
const apiToCall = `${proxyUrl}https://api.darksky.net/forecast/${key}/${lat},${long}`;
// console.log(apiToCall);
fetch(apiToCall)
.then(res => {
return res.json()
})
.then(data => {
console.log(data);
const { temperature, summary, icon} = data.currently;
temperatureDescription.textContent = summary;
temperatureDegree.textContent = temperature;
locationTimezone.textContent = data.timezone;
setIcons(icon, document.querySelector(".icon"))
let cel = (temperature - 32)* (5 / 9);
cel = Math.round(cel*100)/100;;
temperatureSection.addEventListener('click', () => {
if(temperatureSpan.textContent === '°C'){
temperatureSpan.textContent = '°F'
temperatureDegree.textContent = temperature;
}
else{
temperatureSpan.textContent = '°C'
temperatureDegree.textContent = cel;
}
})
})
})
function setIcons(icon, iconID) {
const skycons = new Skycons({color: "white"});
const currentIcon = icon.replace(/-/g, "_").toUpperCase();
skycons.play()
return skycons.set(iconID, Skycons[currentIcon]);
}
}
})