-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
104 lines (92 loc) · 3.25 KB
/
Copy pathscript.js
File metadata and controls
104 lines (92 loc) · 3.25 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
99
100
101
102
103
104
// When user clicks search
document.getElementById("searchBtn").addEventListener("click", () => {
const city = document.getElementById("cityField").value.trim();
if (city) {
getCoordinates(city); // Call coordinate function
} else {
showError("Please enter a city name"); // Show error if empty
}
});
async function getCoordinates(city) {
showError(""); // Clear previous error
try {
const response = await fetch(
`https://geocoding-api.open-meteo.com/v1/search?name=${city}&count=1`
); // API call
if (!response.ok) {
throw new Error("City not found");
}
const data = await response.json(); // Parse JSON
if (!data.results || data.results.length === 0) {
throw new Error("Location not found");
}
const { latitude, longitude, name, country } = data.results[0]; // Extract data
getWeather(latitude, longitude, name, country); // Call weather function
} catch (error) {
showError(error.message); // Show error
}
}
async function getWeather(latitude, longitude, city, country) {
try {
const response = await fetch(
`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}¤t_weather=true`
); // Weather API call
if (!response.ok) {
throw new Error("Weather data not available");
}
const data = await response.json(); // Parse JSON
displayWeather(data.current_weather, city, country);
} catch (error) {
showError(error.message);
}
}
// Weather description and icon
const weatherDescriptions = {
0: "Clear sky",
1: "Mainly clear",
2: "Partly cloudy",
3: "Overcast",
45: "Fog",
48: "Depositing rime fog",
51: "Light drizzle",
53: "Moderate drizzle",
55: "Dense drizzle",
56: "Light freezing drizzle",
57: "Dense freezing drizzle",
61: "Slight rain",
63: "Moderate rain",
65: "Heavy rain",
66: "Light freezing rain",
67: "Heavy freezing rain",
71: "Slight snow fall",
73: "Moderate snow fall",
75: "Heavy snow fall",
77: "Snow grains",
80: "Slight rain showers",
81: "Moderate rain showers",
82: "Violent rain showers",
85: "Slight snow showers",
86: "Heavy snow showers",
95: "Thunderstorm",
96: "Thunderstorm with slight hail",
99: "Thunderstorm with heavy hail"
};
function displayWeather(weather, city, country) {
const weatherContainer = document.getElementById("weatherContainer");
const cityHeader = document.getElementById("cityName");
const temp = document.getElementById("temperature");
const condition = document.getElementById("condition");
const windSpeed = document.getElementById("windSpeed");
const weatherCondition = weatherDescriptions[weather.weathercode] || "Unknown Condition";
weatherContainer.style.display = "block"; // Show weather box
cityHeader.textContent = `${city}, ${country}`; // City display
temp.textContent = `Temperature: ${weather.temperature}°C`;
condition.textContent = `Condition: ${weatherCondition}`;
windSpeed.textContent = `Wind Speed: ${weather.windspeed} km/h`;
}
function showError(message) {
const weatherContainer = document.getElementById("weatherContainer");
weatherContainer.style.display = "none"; // Hide weather box
const errorPara = document.getElementById("errorMessage");
errorPara.textContent = message; // Show error
}