Improve weather widget accuracy using browser geolocation

Update the weather widget to prioritize browser geolocation for more precise location detection, falling back to IP-based geolocation if denied.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 517dfa7b-26ac-463d-a6e1-a58c6df97188
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Event-Id: 16c59ca7-47f2-4f61-8db4-36a7181ab46b
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/f209e72a-0939-48fa-84fc-57854de71967/517dfa7b-26ac-463d-a6e1-a58c6df97188/nFw7xof
Replit-Helium-Checkpoint-Created: true
This commit is contained in:
sebastjanartic 2026-03-02 17:26:23 +00:00
parent bff408393c
commit f099ce8093

View File

@ -176,7 +176,24 @@ export function SidebarWeatherWidget() {
}
}
getLocationByIP();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
async (pos) => {
try {
const res = await fetch(`https://nominatim.openstreetmap.org/reverse?lat=${pos.coords.latitude}&lon=${pos.coords.longitude}&format=json&accept-language=de`);
const geo = await res.json();
const city = geo.address?.city || geo.address?.town || geo.address?.village || "Unbekannt";
fetchWeather(pos.coords.latitude, pos.coords.longitude, city);
} catch {
fetchWeather(pos.coords.latitude, pos.coords.longitude, "Unbekannt");
}
},
() => { getLocationByIP(); },
{ timeout: 5000 }
);
} else {
getLocationByIP();
}
}, []);
const [forecast, setForecast] = useState<{ day: string; high: number; low: number; code: number }[]>([]);