Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update script.js #53

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 72 additions & 43 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,102 @@
// Assigning all the variables and the api Key and Url

const apiKey = "19f175c397782f927e89a4261b9f3ac5";
const apiUrl = "https://api.openweathermap.org/data/2.5/weather?units=metric&q=";
const searchBox = document.querySelector(".searchbox input");
const searchBtn = document.querySelector(".searchbox button")
const searchBtn = document.querySelector(".searchbox button");
const weatherIcon = document.querySelector(".weather-icon");
const currentLocationBtn = document.getElementById("current-location-btn");

// Function to get weather by city name
async function checkWeather(city) {
const response = await fetch(apiUrl + city + `&appid=${apiKey}`);
await handleWeatherResponse(response);
}

async function checkWeather(state) {
const response = await fetch(apiUrl + state + `&appid=${apiKey}`);
await handleWeatherResponse(response);
}

// Function to get weather by latitude and longitude
async function checkWeatherByLocation(lat, lon) {
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?units=metric&lat=${lat}&lon=${lon}&appid=${apiKey}`);
await handleWeatherResponse(response);
}

// Function for the main task (getting the data from API and displaying it)

async function checkWeather(city){

const response = await fetch(apiUrl + city + `&appid=${apiKey}`);
if(response.status == 404){
// Function to handle the API response and update the UI
async function handleWeatherResponse(response) {
if (response.status == 404) {
document.querySelector(".error").style.display = "block";
document.querySelector(".weather").style.display = "none";
searchBox.value = "";
}
else{
} else {
document.querySelector(".error").style.display = "none";
}
var data = await response.json();

document.querySelector(".city").innerHTML = data.name;

document.querySelector(".temperature").innerHTML = Math.round(data.main.temp) + "°c";
const data = await response.json();

document.querySelector(".humidity").innerHTML = data.main.humidity + "%";
document.querySelector(".city").innerHTML = data.name;
document.querySelector(".state").innerHTML = data.name2;
document.querySelector(".temperature").innerHTML = Math.round(data.main.temp) + "°c";
document.querySelector(".humidity").innerHTML = data.main.humidity + "%";
document.querySelector(".wind").innerHTML = data.wind.speed + " kmph";

document.querySelector(".wind").innerHTML = data.wind.speed + " kmph";
// Update the weather icon based on weather conditions
updateWeatherIcon(data.weather[0].main);

// Conditions for to check and display relevant image according to the weather conditions
document.querySelector(".weather").style.display = "block";
}
}

if(data.weather[0].main == "Clouds"){
// Function to update the weather icon based on weather conditions
function updateWeatherIcon(condition) {
if (condition === "Clouds") {
weatherIcon.src = "images/clouds.png";
}
else if(data.weather[0].main == "Clear"){
} else if (condition === "Clear") {
weatherIcon.src = "images/sun.png";
}
else if(data.weather[0].main == "Rain"){
} else if (condition === "Rain") {
weatherIcon.src = "images/rain.png";
}
else if(data.weather[0].main == "Haze"){
} else if (condition === "Haze") {
weatherIcon.src = "images/haze.png";
}
else if(data.weather[0].main == "Drizzle"){
} else if (condition === "Drizzle") {
weatherIcon.src = "images/drizzle.png";
}
else if(data.weather[0].main == "Mist"){
} else if (condition === "Mist") {
weatherIcon.src = "images/mist.png";
}

document.querySelector(".weather").style.display = "block";

// Making the searchBox's value empty once searched
searchBox.value = "";

}

// Event listener for the button

searchBtn.addEventListener("click", ()=>{
// Event listener for the search button
searchBtn.addEventListener("click", () => {
checkWeather(searchBox.value);
})
});

// Enter button(keyCode = 13) event listener

searchBox.addEventListener('keyup', function(event) {
// Event listener for the 'Enter' key press (keyCode = 13)
searchBox.addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
checkWeather(searchBox.value);
}
});
});

// Function to get the user's current location using Geolocation API
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
const lat = position.coords.latitude;
const lon = position.coords.longitude;
checkWeatherByLocation(lat, lon); // Fetch weather for current location
}, () => {
document.querySelector(".error").style.display = "block";
document.querySelector(".error p").textContent = "Unable to retrieve location.";
});
} else {
document.querySelector(".error").style.display = "block";
document.querySelector(".error p").textContent = "Geolocation is not supported by this browser.";
}
}

// Event listener for the current location button
currentLocationBtn.addEventListener("click", getLocation);

// Automatically fetch weather for current location on page load (optional)
// Uncomment the line below if you want to auto-fetch the weather on page load
// window.onload = getLocation;