document.getElementById('search-button').addEventListener('click', function() { let city = document.getElementById('city-input').value; fetchWeatherData(city); }); document.getElementById('location-button').addEventListener('click', function() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(position => { let lat = position.coords.latitude; let lon = position.coords.longitude; fetchWeatherDataByLocation(lat, lon); }); } else { alert('Geolocation is not supported by this browser.'); } }); function fetchWeatherData(city) { fetch(`/weather?city=${city}`) .then(response => response.json()) .then(data => { if (data.error) { alert(data.error); } else { displayWeatherData(data); } }) .catch(error => console.error('Error:', error)); } function fetchWeatherDataByLocation(lat, lon) { fetch(`/weatherlocation?lat=${lat}&lon=${lon}`) .then(response => response.json()) .then(data => { if (data.error) { alert(data.error); } else { displayWeatherData(data); } }) .catch(error => console.error('Error:', error)); } function displayWeatherData(data) { let currentWeather = document.getElementById('current-weather'); currentWeather.innerHTML = `
Temperature: ${data.current.temp_c}°C
Wind: ${data.current.wind_kph} KPH
Humidity: ${data.current.humidity}%
Temp: ${day.day.avgtemp_c}°C
Wind: ${day.day.maxwind_kph} KPH
Humidity: ${day.day.avghumidity}%