-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
60 lines (53 loc) · 2.42 KB
/
Copy pathapp.js
File metadata and controls
60 lines (53 loc) · 2.42 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
//after page is loaded, it can get the location
window.addEventListener('load', ()=> {
let long; //longitue
let lat; //latitude
let temperatureDescription = document.querySelector(".temperature-description");
let temperatureDegree = document.querySelector(".temperature-degree");
let locationTimezone = document.querySelector(".location-timezone");
let temperatureSection = document.querySelector(".degree-section");
const temperatureSpan = document.querySelector(".degree-section span");
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {
// console.log(position);
long = position.coords.longitude;
lat = position.coords.latitude;
const proxy = 'https://cors-anywhere.herokuapp.com/';
const api = `${proxy}https://api.darksky.net/forecast/c1f7514ebf8d611699737dc4fa0619fe/${lat},${long}`;
fetch(api)
.then(response => {
return response.json();
})
.then(data => {
// console.log(data);
// pulls out all the data from currently
const {temperature, summary, icon} = data.currently;
// set DOM elements from the API
temperatureDegree.textContent = temperature;
temperatureDescription.textContent = summary;
locationTimezone.textContent = data.timezone;
//set icon
setIcons(icon, document.querySelector(".icon1"));
//change temperatre to celcius
temperatureSection.addEventListener('click', ()=> {
if(temperatureSpan.textContent === "F") {
temperatureSpan.textContent = "C";
temperatureDegree.textContent = Math.round((temperature - 32) * 5/9);
} else {
temperatureSpan.textContent = "F";
temperatureDegree.textContent = temperature;
}
});
});
});
}
function setIcons(icon, iconID) {
const skycons = new Skycons({color:"white"});
const currentIcon = icon.replace(/-/g, "_").toUpperCase();
skycons.play();
return skycons.set(iconID, Skycons[currentIcon]);
}
function degreeCoverter(temperature) {
return Math.round((temperature - 32) * 5/9);
}
});