Merge branch 'main' of github.com:tatvil/aplicacionesdevanguardia
This commit is contained in:
commit
2c59b145fd
|
|
@ -79,7 +79,7 @@ async function getLocationName(lat, lon) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ==============================================
|
/* ==============================================
|
||||||
TARJETA SOL / ANOCHECER
|
TARJETA AMANECER / ANOCHECER
|
||||||
============================================== */
|
============================================== */
|
||||||
async function getSunTimes(lat, lon) {
|
async function getSunTimes(lat, lon) {
|
||||||
const url = `https://api.sunrise-sunset.org/json?lat=${lat}&lng=${lon}&formatted=0`;
|
const url = `https://api.sunrise-sunset.org/json?lat=${lat}&lng=${lon}&formatted=0`;
|
||||||
|
|
@ -146,26 +146,37 @@ async function updateSunCard(lat, lon) {
|
||||||
/* ==============================================
|
/* ==============================================
|
||||||
TIEMPO ACTUAL
|
TIEMPO ACTUAL
|
||||||
============================================== */
|
============================================== */
|
||||||
|
/* ==============================================
|
||||||
|
TIEMPO ACTUAL (CORREGIDO)
|
||||||
|
============================================== */
|
||||||
async function getWeather(position) {
|
async function getWeather(position) {
|
||||||
const lat = position.coords.latitude;
|
const lat = position.coords.latitude;
|
||||||
const lon = position.coords.longitude;
|
const lon = position.coords.longitude;
|
||||||
|
|
||||||
// Open-Meteo API
|
// Usamos 'current' para obtener datos en tiempo real
|
||||||
const url = `https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lon}¤t_weather=true&temperature_unit=celsius&windspeed_unit=kmh&timezone=auto`;
|
const url = `https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lon}¤t=temperature_2m,relative_humidity_2m,rain,precipitation,wind_speed_10m&timezone=auto`;
|
||||||
|
|
||||||
|
console.log("Fetching weather from:", url);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await fetch(url);
|
const res = await fetch(url);
|
||||||
const data = await res.json();
|
if (!res.ok) throw new Error("Error en la red");
|
||||||
const weather = data.current_weather;
|
|
||||||
|
|
||||||
document.getElementById("temperature").textContent = `${weather.temperature}°C`;
|
const data = await res.json();
|
||||||
document.getElementById("condition").textContent = `Viento ${weather.windspeed} km/h`;
|
|
||||||
document.getElementById("humidity").textContent = "N/A";
|
// La API devuelve los datos dentro de 'current'
|
||||||
document.getElementById("wind").textContent = "N/A";
|
const current = data.current;
|
||||||
|
|
||||||
|
// Inyectamos los datos en tu HTML asegurándonos de que los IDs coinciden
|
||||||
|
document.getElementById("temperature").textContent = `${current.temperature_2m}°C`;
|
||||||
|
document.getElementById("rain").textContent = `${current.rain} mm`;
|
||||||
|
document.getElementById("precipitation").textContent = `${current.precipitation} mm`;
|
||||||
|
document.getElementById("humidity").textContent = `${current.relative_humidity_2m}%`;
|
||||||
|
document.getElementById("wind").textContent = `${current.wind_speed_10m} km/h`;
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
document.getElementById("location").textContent = "Error al obtener el tiempo";
|
console.error("Error en el tiempo:", err);
|
||||||
console.error(err);
|
// Evitamos machacar el ID 'location' para no borrar el nombre de la ciudad
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue