Add Location & User Interactions
10 min read
Connect Browser Geolocation
Task
Turn coordinates into a weather request.
File / section
src/App.jsx — inside useLocation
Change
Use navigator.geolocation.getCurrentPosition(({ coords }) => { const params = new URLSearchParams({ lat: coords.latitude, lon: coords.longitude, appid: API_KEY, units: unit }); loadWeather(${API_BASE}/weather?${params}); }, ... ).
Why this change matters
The geolocation callback supplies latitude and longitude, which can be sent directly to OpenWeather.
Test it
Allow location access and confirm the returned location appears in WeatherNow.
Checkpoint
Browser coordinates now load live weather.
navigator.geolocation.getCurrentPosition(
({ coords }) => {
const params = new URLSearchParams({
lat: coords.latitude,
lon: coords.longitude,
appid: API_KEY,
units: unit
});
loadWeather(`${API_BASE}/weather?${params}`);
},
() => setError("Location permission was denied or unavailable."),
{ enableHighAccuracy: true, timeout: 10000 }
);