fix api
This commit is contained in:
@@ -15,88 +15,6 @@ const loading = document.getElementById('loading');
|
||||
const weatherContainer = document.getElementById('weatherContainer');
|
||||
|
||||
|
||||
/**
|
||||
* FUNZIONE: Gestione errori
|
||||
*
|
||||
* Mostra un messaggio di errore e logga in console
|
||||
*/
|
||||
function handleError(message) {
|
||||
weatherContainer.innerHTML = `
|
||||
<div class="error">
|
||||
<strong>❌ ${message}</strong>
|
||||
</div>
|
||||
`;
|
||||
console.error('Errore:', message);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* FUNZIONE: Ricerca il meteo per latitudine e longitudine
|
||||
*
|
||||
* Parametri obbligatori dell'API:
|
||||
* - latitude: numero decimale
|
||||
* - longitude: numero decimale
|
||||
* - current: variabili da ottenere (separati da virgola)
|
||||
* - timezone: 'auto' oppure fuso orario specifico
|
||||
*
|
||||
* Passi:
|
||||
* 1. Leggi latitudine e longitudine dagli input
|
||||
* 2. Valida che siano compilati
|
||||
* 3. Mostra lo spinner di caricamento
|
||||
* 4. Costruisci l'URL con i parametri corretti
|
||||
* 5. Fai una GET a https://api.open-meteo.com/v1/forecast
|
||||
* 6. Se non OK, mostra errore e return
|
||||
* 7. Converti in JSON
|
||||
* 8. Estrai data.current
|
||||
* 9. Chiama displayWeather() per visualizzare
|
||||
* 10. Nascondi lo spinner
|
||||
*/
|
||||
async function searchWeather() {
|
||||
const lat = latitude.value;
|
||||
const lon = longitude.value;
|
||||
|
||||
// VALIDAZIONE
|
||||
if (!lat || !lon) {
|
||||
handleError('Inserisci latitudine e longitudine');
|
||||
return;
|
||||
}
|
||||
|
||||
loading.classList.remove('nascosto');
|
||||
weatherContainer.innerHTML = '';
|
||||
|
||||
try {
|
||||
// 👇 SCRIVI QUI IL TUO CODICE 👇
|
||||
|
||||
// 1. Costruisci l'URL con i parametri corretti:
|
||||
// const url = 'https://api.open-meteo.com/v1/forecast' +
|
||||
// '?latitude=' + lat +
|
||||
// '&longitude=' + lon +
|
||||
// '¤t=temperature_2m,relative_humidity_2m,weather_code' +
|
||||
// '&timezone=auto';
|
||||
|
||||
// 2. Fai la fetch
|
||||
// const response = await fetch(url);
|
||||
|
||||
// 3. Se non OK, mostra errore e return
|
||||
// if (!response.ok) {
|
||||
// throw new Error('Errore nel caricamento dei dati meteo');
|
||||
// }
|
||||
|
||||
// 4. Converti in JSON
|
||||
// const data = await response.json();
|
||||
|
||||
// 5. Estrai i dati meteo e visualizza
|
||||
// displayWeather(data.current, lat, lon);
|
||||
|
||||
throw new Error('Codice non implementato - Completa searchWeather()');
|
||||
|
||||
} catch (error) {
|
||||
handleError(error.message);
|
||||
} finally {
|
||||
loading.classList.add('nascosto');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* FUNZIONE: Visualizza i dati meteo
|
||||
* (Questa funzione è già fatta - non modificare)
|
||||
@@ -122,7 +40,7 @@ function displayWeather(current, lat, lon) {
|
||||
|
||||
const weather = weatherDescriptions[current.weather_code] || { emoji: '❓', descrizione: 'Sconosciuto' };
|
||||
|
||||
const html = `
|
||||
weatherContainer.innerHTML = `
|
||||
<div class="weather-card">
|
||||
<div class="location">
|
||||
📍 ${lat.toFixed(2)}°N, ${lon.toFixed(2)}°E
|
||||
@@ -140,10 +58,66 @@ function displayWeather(current, lat, lon) {
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
weatherContainer.innerHTML = html;
|
||||
}
|
||||
|
||||
/**
|
||||
* FUNZIONE: Gestione errori
|
||||
* Mostra un messaggio di errore e logga in console
|
||||
* Funzione già fatta - non modificare
|
||||
*/
|
||||
function mostraErrore(message) {
|
||||
weatherContainer.innerHTML = `
|
||||
<div class="error">
|
||||
<strong>❌ ${message}</strong>
|
||||
</div>
|
||||
`;
|
||||
console.error('Errore:', message);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* FUNZIONE: Ricerca il meteo per latitudine e longitudine
|
||||
*
|
||||
* Parametri obbligatori dell'API:
|
||||
* - latitude: numero decimale
|
||||
* - longitude: numero decimale
|
||||
* - current: variabili da ottenere (separati da virgola)
|
||||
* - timezone: 'auto' oppure fuso orario specifico
|
||||
*/
|
||||
async function searchWeather() {
|
||||
const lat = latitude.value;
|
||||
const lon = longitude.value;
|
||||
|
||||
// VALIDAZIONE
|
||||
if (!lat || !lon) {
|
||||
mostraErrore('Inserisci latitudine e longitudine');
|
||||
return;
|
||||
}
|
||||
|
||||
loading.classList.remove('nascosto');
|
||||
weatherContainer.innerHTML = '';
|
||||
|
||||
try {
|
||||
// 1. Costruisci l'URL `https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lon}¤t=temperature_2m,relative_humidity_2m,weather_code&timezone=auto`
|
||||
|
||||
// 2. Fai la fetch
|
||||
|
||||
// 3. Se non OK, lancia un errore con lo status code
|
||||
|
||||
// 4. Converti in JSON
|
||||
|
||||
// 5. Estrai i dati meteo e visualizza con displayWeather()
|
||||
|
||||
throw new Error('Codice non implementato - Completa searchWeather()'); // Da Rimuovere
|
||||
|
||||
} catch (error) {
|
||||
mostraErrore(error.message);
|
||||
}
|
||||
|
||||
loading.classList.add('nascosto');
|
||||
}
|
||||
|
||||
|
||||
// ===== COLLEGA GLI EVENTI =====
|
||||
btnSearch.addEventListener('click', searchWeather);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user