-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
68 lines (55 loc) · 2.67 KB
/
script.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
function openPopUp() {
document.querySelector(".alert").style.display = "none";
document.querySelector("body").style.backgroundColor = "#5397e2";
setTimeout(() => {
console.log("Updating Weather");
document.querySelector(".app").style.display = "block";
}, "1000");
}
function warning() {
document.querySelector(".alert").style.display = "none";
setTimeout(() => {
alert("Can't update weather without permissions!");
}, "100");
setTimeout(() => {
document.querySelector(".alert").style.display = "block";
}, "500");
}
const getWeather = async (apiURL) => {
const response = await fetch(apiURL);
const data = await response.json();
console.log(data);
document.querySelector(".city").innerHTML = data.name;
document.querySelector(".temp h1").innerHTML = ` ${Math.round(data.main.temp)}` + " °";
document.querySelector(".min-max").innerHTML = `${Math.round(data.main.temp_min)}°/${Math.round(data.main.temp_max)}° Feels like ${Math.round(data.main.feels_like)}°`;
document.querySelector(".weather").innerHTML = data.weather[0].main;
document.querySelector(".value1").innerHTML = data.main.humidity +" %";
document.querySelector(".value2").innerHTML = data.main.pressure +" mb";
document.querySelector(".value3").innerHTML = data.wind.speed + " kph";
document.querySelector(".value4").innerHTML = data.wind.deg + " °";
document.querySelector(".value5").innerHTML = "NA";
document.querySelector(".value6").innerHTML = data.visibility +" m";
document.querySelector(".value7").innerHTML = "NA";
if (data.weather[0].main === "Mist") {
document.querySelector(".temp img").src = "images/mist.png";
} else if (data.weather[0].main === "Snow") {
document.querySelector(".temp img").src = "images/snow.png";
} else if (data.weather[0].main === "Rain") {
document.querySelector(".temp img").src = "images/rain.png";
} else if (data.weather[0].main === "Clear") {
document.querySelector(".temp img").src = "images/clear.png";
} else if (data.weather[0].main === "Clouds") {
document.querySelector(".temp img").src = "images/clouds.png";
} else if (data.weather[0].main === "Drizzle") {
document.querySelector(".temp img").src = "images/drizzle.png";
} else if (data.weather[0].main === "Haze") {
document.querySelector(".temp img").src = "images/humidity.png";
}
};
navigator.geolocation.getCurrentPosition((position) => {
const lat = position.coords.latitude;
const lon = position.coords.longitude;
const apiKey = "9f94cbe309e8a40379e036219f8e9718";
const apiURL = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${apiKey}` + "&units=metric";
getWeather(apiURL);
});