-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
66 lines (59 loc) · 2.21 KB
/
app.js
File metadata and controls
66 lines (59 loc) · 2.21 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
61
62
63
64
65
66
window.addEventListener("load", () => {
let long;
let lat;
let temperatureDescription = document.querySelector(
".temperature-description"
);
let weatherImg = document.querySelector(".icon");
let temperatureDegree = document.querySelector(".temperature-degree");
let locationName = document.querySelector(".location-name");
let locationRegion = document.querySelector(".location-region");
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
long = position.coords.longitude;
lat = position.coords.latitude;
const api = `http://api.weatherapi.com/v1/current.json?key=165b6354acb147fb92e121913212109&q=${lat},%20${long}&aqi=no`;
console.log(lat);
console.log(long);
console.log("dynamic API:", api);
// console.log("static API:", apiStatic);
fetch(api)
.then((response) => {
return response.json();
})
.then((data) => {
console.log(data);
const getLocation = data.location.name;
const getRegion = data.location.region;
const currentTemp = data.current.temp_c;
const summary = data.current.condition.text;
const weatherIcon = data.current.condition.icon;
const currentTime = data.location.localtime.split(" ")[1];
console.log(
getLocation,
currentTemp,
summary,
weatherIcon,
getRegion,
currentTime
);
//* Set DOM elements from API
locationName.innerText = getLocation;
// locationRegion.innerText = getRegion;
weatherImg.src = weatherIcon;
temperatureDescription.innerText = summary;
temperatureDegree.innerText = currentTemp;
locationRegion.innerText = getRegion;
if (summary === "Overcast") {
document.body.classList.add("overcast-background");
} else if (summary === "Sunny") {
document.body.classList.add("sunny-background");
} else if (summary === "Cloudy") {
document.body.classList.add("cloudy-background");
}
});
});
} else {
h1.textContent = "Location not available";
}
});