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:
parent
0024ce36c8
commit
0bb8b7ec5c
1 changed files with 34 additions and 24 deletions
|
|
@ -31,30 +31,40 @@ const WeatherWidget = () => {
|
||||||
city: string;
|
city: string;
|
||||||
}) => void,
|
}) => void,
|
||||||
) => {
|
) => {
|
||||||
/*
|
if (navigator.geolocation) {
|
||||||
// Geolocation doesn't give city so we'll country using ipapi for now
|
const result = await navigator.permissions.query({
|
||||||
if (navigator.geolocation) {
|
name: 'geolocation',
|
||||||
const result = await navigator.permissions.query({
|
});
|
||||||
name: 'geolocation',
|
|
||||||
})
|
|
||||||
|
|
||||||
if (result.state === 'granted') {
|
if (result.state === 'granted') {
|
||||||
navigator.geolocation.getCurrentPosition(position => {
|
navigator.geolocation.getCurrentPosition(async (position) => {
|
||||||
callback({
|
const res = await fetch(
|
||||||
latitude: position.coords.latitude,
|
`https://api-bdc.io/data/reverse-geocode-client?latitude=${position.coords.latitude}&longitude=${position.coords.longitude}&localityLanguage=en`,
|
||||||
longitude: position.coords.longitude,
|
{
|
||||||
})
|
method: 'GET',
|
||||||
})
|
headers: {
|
||||||
} else if (result.state === 'prompt') {
|
'Content-Type': 'application/json',
|
||||||
callback(await getApproxLocation())
|
},
|
||||||
navigator.geolocation.getCurrentPosition(position => {})
|
},
|
||||||
} else if (result.state === 'denied') {
|
);
|
||||||
callback(await getApproxLocation())
|
|
||||||
}
|
const data = await res.json();
|
||||||
} else {
|
|
||||||
callback(await getApproxLocation())
|
callback({
|
||||||
} */
|
latitude: position.coords.latitude,
|
||||||
callback(await getApproxLocation());
|
longitude: position.coords.longitude,
|
||||||
|
city: data.locality,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} else if (result.state === 'prompt') {
|
||||||
|
callback(await getApproxLocation());
|
||||||
|
navigator.geolocation.getCurrentPosition((position) => {});
|
||||||
|
} else if (result.state === 'denied') {
|
||||||
|
callback(await getApproxLocation());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
callback(await getApproxLocation());
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
getLocation(async (location) => {
|
getLocation(async (location) => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue