diff --git a/JavaScript/AqiDetector/README.md b/JavaScript/AqiDetector/README.md new file mode 100644 index 00000000..92a4401e --- /dev/null +++ b/JavaScript/AqiDetector/README.md @@ -0,0 +1,6 @@ +## AQI Detector: +- This script built in Javascript is a AQI Detector. +- The script automatically detects the location of the current city or the nearest city. +- If the browser supports the geolocation feature, it didplays AQI and the location,else error message is displayed. + +- Output of the script. \ No newline at end of file diff --git a/JavaScript/AqiDetector/app.js b/JavaScript/AqiDetector/app.js new file mode 100644 index 00000000..95351531 --- /dev/null +++ b/JavaScript/AqiDetector/app.js @@ -0,0 +1,32 @@ +var temp = { + key: "943422edf7ee41eb9b0ba3854e628f08 ", + base: "https://api.weatherbit.io/v2.0/current/airquality?" + } +var notificationElement = document.querySelector(".notification"); +if (navigator.geolocation) { + navigator.geolocation.getCurrentPosition(setPosition); + notificationElement.innerHTML = ""; + +} +else { + notificationElement.innerHTML = "Geolocation is not supported by this browser."; + } +function setPosition(position){ + let latitude = position.coords.latitude; + let longitude = position.coords.longitude; + console.log(latitude); + console.log(longitude); +getAQI(latitude, longitude); + } +function getAQI(lat,lon) { + fetch(`${temp.base}lat=${lat}&lon=${lon}&units=metric&key=${temp.key}`) + .then(aqi => { + return aqi.json(); + }).then(displayResults); +} +function displayResults(aq){ + let aqi=document.querySelector(".aqi"); + aqi.innerHTML=`${aq.data[0].aqi}`; + let location=document.querySelector(".location"); + location.innerHTML=`${aq.city_name},${aq.country_code}` +} \ No newline at end of file diff --git a/JavaScript/AqiDetector/capture.JPG b/JavaScript/AqiDetector/capture.JPG new file mode 100644 index 00000000..8ba740ad Binary files /dev/null and b/JavaScript/AqiDetector/capture.JPG differ diff --git a/JavaScript/AqiDetector/index.html b/JavaScript/AqiDetector/index.html new file mode 100644 index 00000000..901dc5ca --- /dev/null +++ b/JavaScript/AqiDetector/index.html @@ -0,0 +1,24 @@ + + + + + + +
+ + +