-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
56 lines (47 loc) · 2.27 KB
/
script.js
File metadata and controls
56 lines (47 loc) · 2.27 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
async function getCoordinates(city) {
try {
let geoResponse = await fetch(`https://geocoding-api.open-meteo.com/v1/search?name=${city}&count=1&language=en&format=json`);
let geoData = await geoResponse.json();
if (!geoData.results || geoData.results.length === 0) {
throw new Error("City not found! ❌");
}
let { latitude: lat, longitude: lon } = geoData.results[0]; // Extract latitude & longitude
console.log(`📍 Coordinates for ${city}: ${lat}, ${lon}`);
return { lat, lon }; // Pass data to the next function
} catch (error) {
document.getElementById("errorMessage").innerText = error.message;
console.error("Error fetching coordinates:", error);
}
}
async function getWeather(lat, lon) {
try {
let weatherResponse = await fetch(`https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lon}¤t_weather=true`);
let weatherData = await weatherResponse.json();
if (!weatherData.current_weather) {
throw new Error("Weather data not found! ❌");
}
// Display data in UI
document.getElementById("temperature").innerText = `${weatherData.current_weather.temperature}°C`;
document.getElementById("wind-speed").innerText = `${weatherData.current_weather.windspeed} km/h`;
document.getElementById("errorMessage").innerText = ""; // Clear errors
} catch (error) {
document.getElementById("errorMessage").innerText = error.message;
console.error("Error fetching weather:", error);
}
}
async function fetchWeatherForCity(city) {
let coordinates = await getCoordinates(city);
if (coordinates) {
document.getElementById("city-name").innerText = city; // Update city name
await getWeather(coordinates.lat, coordinates.lon); // Pass values to the next API call
}
}
// Event Listener for Button Click
document.getElementById("getWeatherBtn").addEventListener("click", () => {
let city = document.getElementById("cityInput").value.trim();
if (city) {
fetchWeatherForCity(city);
} else {
document.getElementById("errorMessage").innerText = "Please enter a city name!";
}
});