-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
48 lines (42 loc) · 1.77 KB
/
app.js
File metadata and controls
48 lines (42 loc) · 1.77 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
"use strict";
window.addEventListener('load', () => {
let latitudeCity;
let longitudeCity;
let temperatureTitle = document.querySelector('.nav__title');
let temperatureTemp = document.querySelector('.temp');
let temperatureCity = document.querySelector('.city');
let temperatureHour = document.querySelector('.hour');
let time = new Date();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {
latitudeCity = position.coords.latitude;
longitudeCity = position.coords.longitude;
let proxy = 'https://cors-anywhere.herokuapp.com/';/*This API enables cross-origin requests to anywhere. - Just for localhost*/
const apiDarksky = `${proxy}https://api.darksky.net/forecast/d39c252c5879663dacf8bd1078b6ba60/${latitudeCity},${longitudeCity}`;
fetch(apiDarksky)
.then(response => {
return response.json();
})
.then(dataWeather => {
const {icon, temperature, summary } = dataWeather.currently;
setIcons(icon, document.querySelector(".icon"));
temperatureTitle.textContent = summary;
// Rounding numbers to 2 digits after comma + convert F to C
let temperatureC = (5/9) * (temperature - 32);
let temp = Number(temperatureC);
let convertFtoC = temp.toFixed(2);
temperatureTemp.textContent = Number(convertFtoC) + "°C" ;
temperatureCity.textContent = dataWeather.timezone;
temperatureHour.textContent = time.getHours() + ":"
+ (time.getMinutes()<10?'0':'')
+ time.getMinutes(); // display two digit numbers - getMinutes() 0-9
})
});
function setIcons(icon, iconID) {
const skycons = new Skycons({color: "#444"});
const currentIcon = icon.replace(/-/g, "_").toUpperCase();
skycons.play();
return skycons.set(iconID, Skycons[currentIcon]);
};
}
});