-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
98 lines (82 loc) · 2.65 KB
/
Copy pathscript.js
File metadata and controls
98 lines (82 loc) · 2.65 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const errorLabel = document.querySelector("label[for='error-msg']")
const latInp = document.querySelector("#latitude")
const lonInp = document.querySelector("#longitude")
const airQuality = document.querySelector(".air-quality")
const airQualityStat = document.querySelector(".air-quality-status")
const srchBtn = document.querySelector(".search-btn")
const componentsEle = document.querySelectorAll(".component-val")
const appId = "cfe29cbe0bdade1f831edb7df7e734a0"
const link = "https://api.openweathermap.org/data/2.5/air_pollution" // API end point
const getUserLocation = () => {
// Get user Location
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(onPositionGathered, onPositionGatherError)
} else {
onPositionGatherError({ message: "Can't Access your location. Please enter your co-ordinates" })
}
}
const onPositionGathered = (pos) => {
let lat = pos.coords.latitude.toFixed(4), lon = pos.coords.longitude.toFixed(4)
// Set values of Input for user to know
latInp.value = lat
lonInp.value = lon
// Get Air data from weather API
getAirQuality(lat, lon)
}
const getAirQuality = async (lat, lon) => {
// Get data from api
const rawData = await fetch(`${link}?lat=${lat}&lon=${lon}&appid=${appId}`).catch(err => {
onPositionGatherError({ message: "Something went wrong. Check your internet conection." })
console.log(err)
})
const airData = await rawData.json()
setValuesOfAir(airData)
setComponentsOfAir(airData)
}
const setValuesOfAir = airData => {
const aqi = airData.list[0].main.aqi
let airStat = "", color = ""
// Set Air Quality Index
airQuality.innerText = aqi
// Set status of air quality
switch (aqi) {
case 1:
airStat = "Good"
color = "rgb(19, 201, 28)"
break
case 2:
airStat = "Fair"
color = "rgb(15, 134, 25)"
break
case 3:
airStat = "Moderate"
color = "rgb(201, 204, 13)"
break
case 4:
airStat = "Poor"
color = "rgb(204, 83, 13)"
break
case 5:
airStat = "Very Poor"
color = "rgb(204, 13, 13)"
break
default:
airStat = "Unknown"
}
airQualityStat.innerText = airStat
airQualityStat.style.color = color
}
const setComponentsOfAir = airData => {
let components = {...airData.list[0].components}
componentsEle.forEach(ele => {
const attr = ele.getAttribute('data-comp')
ele.innerText = components[attr] += " μg/m³"
})
}
const onPositionGatherError = e => {
errorLabel.innerText = e.message
}
srchBtn.addEventListener("click", () => {
getAirQuality(parseFloat(latInp.value).toFixed(4), parseFloat(lonInp.value).toFixed(4))
})
getUserLocation()