feat(weather-widget): enable geolocation for weather data

Replaces the previous commented-out geolocation logic with an implementation that uses the browser's geolocation API and reverse geocoding to determine the user's city. Falls back to approximate location if permission is denied or unavailable.
This commit is contained in:
ItzCrazyKns
2025-06-28 13:49:17 +05:30
parent 0024ce36c8
commit 0bb8b7ec5c

View File

@ -31,30 +31,40 @@ const WeatherWidget = () => {
city: string;
}) => void,
) => {
/*
// Geolocation doesn't give city so we'll country using ipapi for now
if (navigator.geolocation) {
const result = await navigator.permissions.query({
name: 'geolocation',
})
});
if (result.state === 'granted') {
navigator.geolocation.getCurrentPosition(position => {
navigator.geolocation.getCurrentPosition(async (position) => {
const res = await fetch(
`https://api-bdc.io/data/reverse-geocode-client?latitude=${position.coords.latitude}&longitude=${position.coords.longitude}&localityLanguage=en`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
},
);
const data = await res.json();
callback({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
})
})
city: data.locality,
});
});
} else if (result.state === 'prompt') {
callback(await getApproxLocation())
navigator.geolocation.getCurrentPosition(position => {})
callback(await getApproxLocation());
navigator.geolocation.getCurrentPosition((position) => {});
} else if (result.state === 'denied') {
callback(await getApproxLocation())
callback(await getApproxLocation());
}
} else {
callback(await getApproxLocation())
} */
callback(await getApproxLocation());
}
};
getLocation(async (location) => {