Unable to search for any city
Browse files- index.html +27 -65
index.html
CHANGED
|
@@ -151,19 +151,20 @@
|
|
| 151 |
try {
|
| 152 |
// Fetch current weather
|
| 153 |
// First get coordinates for location
|
| 154 |
-
|
|
|
|
| 155 |
if (!geoResponse.ok) throw new Error('Location service unavailable');
|
| 156 |
const geoData = await geoResponse.json();
|
| 157 |
|
| 158 |
-
if (!geoData
|
| 159 |
throw new Error('City not found. Try a different spelling or nearby location.');
|
| 160 |
}
|
| 161 |
|
| 162 |
-
const { latitude, longitude } = geoData
|
| 163 |
-
|
| 164 |
-
//
|
| 165 |
-
const weatherResponse = await fetch(`https://api.
|
| 166 |
-
|
| 167 |
const weatherData = await weatherResponse.json();
|
| 168 |
|
| 169 |
// Validate we got data
|
|
@@ -188,82 +189,43 @@
|
|
| 188 |
}
|
| 189 |
}
|
| 190 |
function updateCurrentWeather(data) {
|
| 191 |
-
const current = data.
|
| 192 |
-
const date = new Date(current.
|
| 193 |
|
| 194 |
document.getElementById('location').textContent = document.getElementById('location-input').value;
|
| 195 |
document.getElementById('date').textContent = date.toLocaleDateString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' });
|
| 196 |
-
document.getElementById('temp').textContent = `${Math.round(current.
|
| 197 |
-
|
| 198 |
-
// Map weather codes to descriptions
|
| 199 |
-
const weatherCodes = {
|
| 200 |
-
0: 'Clear sky',
|
| 201 |
-
1: 'Mainly clear',
|
| 202 |
-
2: 'Partly cloudy',
|
| 203 |
-
3: 'Overcast',
|
| 204 |
-
45: 'Fog',
|
| 205 |
-
48: 'Depositing rime fog',
|
| 206 |
-
51: 'Light drizzle',
|
| 207 |
-
53: 'Moderate drizzle',
|
| 208 |
-
55: 'Dense drizzle',
|
| 209 |
-
56: 'Light freezing drizzle',
|
| 210 |
-
57: 'Dense freezing drizzle',
|
| 211 |
-
61: 'Slight rain',
|
| 212 |
-
63: 'Moderate rain',
|
| 213 |
-
65: 'Heavy rain',
|
| 214 |
-
66: 'Light freezing rain',
|
| 215 |
-
67: 'Heavy freezing rain',
|
| 216 |
-
71: 'Slight snow fall',
|
| 217 |
-
73: 'Moderate snow fall',
|
| 218 |
-
75: 'Heavy snow fall',
|
| 219 |
-
77: 'Snow grains',
|
| 220 |
-
80: 'Slight rain showers',
|
| 221 |
-
81: 'Moderate rain showers',
|
| 222 |
-
82: 'Violent rain showers',
|
| 223 |
-
85: 'Slight snow showers',
|
| 224 |
-
86: 'Heavy snow showers',
|
| 225 |
-
95: 'Thunderstorm',
|
| 226 |
-
96: 'Thunderstorm with slight hail',
|
| 227 |
-
99: 'Thunderstorm with heavy hail'
|
| 228 |
-
};
|
| 229 |
|
| 230 |
-
const weatherDesc =
|
| 231 |
-
document.getElementById('weather-desc').textContent = weatherDesc;
|
| 232 |
-
document.getElementById('wind-speed').textContent = `${current.
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
-
// Update weather icon with
|
| 235 |
const weatherIcon = document.getElementById('weather-icon');
|
| 236 |
-
weatherIcon.innerHTML = `<img src="
|
| 237 |
}
|
| 238 |
function updateForecast(data) {
|
| 239 |
const forecastContainer = document.getElementById('forecast');
|
| 240 |
forecastContainer.innerHTML = '';
|
| 241 |
-
|
| 242 |
-
const weatherCodes = {
|
| 243 |
-
0: 'Clear', 1: 'Mainly clear', 2: 'Partly cloudy', 3: 'Overcast',
|
| 244 |
-
45: 'Fog', 48: 'Rime fog', 51: 'Drizzle', 53: 'Drizzle', 55: 'Drizzle',
|
| 245 |
-
61: 'Rain', 63: 'Rain', 65: 'Rain', 80: 'Showers', 81: 'Showers', 82: 'Showers',
|
| 246 |
-
71: 'Snow', 73: 'Snow', 75: 'Snow', 85: 'Snow', 86: 'Snow',
|
| 247 |
-
95: 'Thunderstorm', 96: 'Thunderstorm', 99: 'Thunderstorm'
|
| 248 |
-
};
|
| 249 |
-
|
| 250 |
for (let i = 0; i < 5; i++) {
|
| 251 |
-
const
|
|
|
|
| 252 |
const dayName = date.toLocaleDateString('en-US', { weekday: 'short' });
|
| 253 |
-
const
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
const forecastItem = document.createElement('div');
|
| 257 |
forecastItem.className = 'weather-card p-4 text-center forecast-item';
|
| 258 |
forecastItem.innerHTML = `
|
| 259 |
<p class="font-medium text-gray-800">${dayName}</p>
|
| 260 |
<p class="text-sm text-gray-600 mb-2">${date.toLocaleDateString('en-US', { month: 'short', day: 'numeric' })}</p>
|
| 261 |
-
<img src="
|
| 262 |
<p class="text-gray-600 text-sm capitalize">${weatherDesc}</p>
|
| 263 |
<div class="flex justify-center gap-4 mt-2">
|
| 264 |
-
<span class="font-bold text-gray-800">${Math.round(
|
| 265 |
-
<span class="text-gray-600">${Math.round(
|
| 266 |
-
|
| 267 |
`;
|
| 268 |
forecastContainer.appendChild(forecastItem);
|
| 269 |
}
|
|
|
|
| 151 |
try {
|
| 152 |
// Fetch current weather
|
| 153 |
// First get coordinates for location
|
| 154 |
+
// Use OpenWeatherMap geocoding API which is more reliable
|
| 155 |
+
const geoResponse = await fetch(`https://api.openweathermap.org/geo/1.0/direct?q=${location}&limit=1&appid=3fd7e0e3b5f5bf7f3a6cee1c6af5f8e0`);
|
| 156 |
if (!geoResponse.ok) throw new Error('Location service unavailable');
|
| 157 |
const geoData = await geoResponse.json();
|
| 158 |
|
| 159 |
+
if (!geoData || geoData.length === 0) {
|
| 160 |
throw new Error('City not found. Try a different spelling or nearby location.');
|
| 161 |
}
|
| 162 |
|
| 163 |
+
const { lat: latitude, lon: longitude } = geoData[0];
|
| 164 |
+
// Fetch current weather and forecast in one call
|
| 165 |
+
// Use OpenWeatherMap API for weather data
|
| 166 |
+
const weatherResponse = await fetch(`https://api.openweathermap.org/data/2.5/onecall?lat=${latitude}&lon=${longitude}&exclude=minutely,hourly,alerts&units=metric&appid=3fd7e0e3b5f5bf7f3a6cee1c6af5f8e0`);
|
| 167 |
+
if (!weatherResponse.ok) throw new Error('Weather service unavailable');
|
| 168 |
const weatherData = await weatherResponse.json();
|
| 169 |
|
| 170 |
// Validate we got data
|
|
|
|
| 189 |
}
|
| 190 |
}
|
| 191 |
function updateCurrentWeather(data) {
|
| 192 |
+
const current = data.current;
|
| 193 |
+
const date = new Date(current.dt * 1000);
|
| 194 |
|
| 195 |
document.getElementById('location').textContent = document.getElementById('location-input').value;
|
| 196 |
document.getElementById('date').textContent = date.toLocaleDateString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' });
|
| 197 |
+
document.getElementById('temp').textContent = `${Math.round(current.temp)}°C`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
+
const weatherDesc = current.weather[0].description;
|
| 200 |
+
document.getElementById('weather-desc').textContent = weatherDesc.charAt(0).toUpperCase() + weatherDesc.slice(1);
|
| 201 |
+
document.getElementById('wind-speed').textContent = `${current.wind_speed} km/h`;
|
| 202 |
+
document.getElementById('humidity').textContent = `${current.humidity}%`;
|
| 203 |
+
document.getElementById('pressure').textContent = `${current.pressure} hPa`;
|
| 204 |
+
document.getElementById('visibility').textContent = `${current.visibility / 1000} km`;
|
| 205 |
|
| 206 |
+
// Update weather icon with OpenWeatherMap icon
|
| 207 |
const weatherIcon = document.getElementById('weather-icon');
|
| 208 |
+
weatherIcon.innerHTML = `<img src="https://openweathermap.org/img/wn/${current.weather[0].icon}@2x.png" alt="${weatherDesc}">`;
|
| 209 |
}
|
| 210 |
function updateForecast(data) {
|
| 211 |
const forecastContainer = document.getElementById('forecast');
|
| 212 |
forecastContainer.innerHTML = '';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
for (let i = 0; i < 5; i++) {
|
| 214 |
+
const forecast = data.daily[i];
|
| 215 |
+
const date = new Date(forecast.dt * 1000);
|
| 216 |
const dayName = date.toLocaleDateString('en-US', { weekday: 'short' });
|
| 217 |
+
const weatherDesc = forecast.weather[0].description;
|
| 218 |
+
const forecastItem = document.createElement('div');
|
|
|
|
|
|
|
| 219 |
forecastItem.className = 'weather-card p-4 text-center forecast-item';
|
| 220 |
forecastItem.innerHTML = `
|
| 221 |
<p class="font-medium text-gray-800">${dayName}</p>
|
| 222 |
<p class="text-sm text-gray-600 mb-2">${date.toLocaleDateString('en-US', { month: 'short', day: 'numeric' })}</p>
|
| 223 |
+
<img src="https://openweathermap.org/img/wn/${forecast.weather[0].icon}@2x.png" alt="${weatherDesc}" class="mx-auto w-16 h-16">
|
| 224 |
<p class="text-gray-600 text-sm capitalize">${weatherDesc}</p>
|
| 225 |
<div class="flex justify-center gap-4 mt-2">
|
| 226 |
+
<span class="font-bold text-gray-800">${Math.round(forecast.temp.max)}°</span>
|
| 227 |
+
<span class="text-gray-600">${Math.round(forecast.temp.min)}°</span>
|
| 228 |
+
</div>
|
| 229 |
`;
|
| 230 |
forecastContainer.appendChild(forecastItem);
|
| 231 |
}
|