fix: react weather location + nominatim fallback (#1290)
This commit is contained in:
parent
377778596a
commit
60de5a129a
1 changed files with 33 additions and 9 deletions
|
|
@ -54,18 +54,35 @@ Singleton {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const [lat, lon] = coords.split(",");
|
const [lat, lon] = coords.split(",").map(s => s.trim());
|
||||||
const url = `https://nominatim.openstreetmap.org/reverse?lat=${lat}&lon=${lon}&format=geocodejson`;
|
|
||||||
Requests.get(url, text => {
|
const fallbackToBigDataCloud = () => {
|
||||||
const geo = JSON.parse(text).features?.[0]?.properties.geocoding;
|
const fallbackUrl = `https://api.bigdatacloud.net/data/reverse-geocode-client?latitude=${lat}&longitude=${lon}&localityLanguage=en`;
|
||||||
if (geo) {
|
Requests.get(fallbackUrl, text => {
|
||||||
const geoCity = geo.type === "city" ? geo.name : geo.city;
|
const geo = JSON.parse(text);
|
||||||
|
const geoCity = geo.city || geo.locality;
|
||||||
|
if (geoCity) {
|
||||||
city = geoCity;
|
city = geoCity;
|
||||||
cachedCities.set(coords, geoCity);
|
cachedCities.set(coords, geoCity);
|
||||||
} else {
|
} else {
|
||||||
city = "Unknown City";
|
city = "Unknown City";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const nominatimUrl = `https://nominatim.openstreetmap.org/reverse?lat=${lat}&lon=${lon}&format=geocodejson`;
|
||||||
|
Requests.get(nominatimUrl, text => {
|
||||||
|
const geo = JSON.parse(text).features?.[0]?.properties.geocoding;
|
||||||
|
if (geo) {
|
||||||
|
const geoCity = geo.type === "city" ? geo.name : geo.city;
|
||||||
|
if (geoCity) {
|
||||||
|
city = geoCity;
|
||||||
|
cachedCities.set(coords, geoCity);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fallbackToBigDataCloud();
|
||||||
|
}, fallbackToBigDataCloud);
|
||||||
}
|
}
|
||||||
|
|
||||||
function fetchCoordsFromCity(cityName: string): void {
|
function fetchCoordsFromCity(cityName: string): void {
|
||||||
|
|
@ -149,7 +166,7 @@ Singleton {
|
||||||
if (!loc || loc.indexOf(",") === -1)
|
if (!loc || loc.indexOf(",") === -1)
|
||||||
return "";
|
return "";
|
||||||
|
|
||||||
const [lat, lon] = loc.split(",");
|
const [lat, lon] = loc.split(",").map(s => s.trim());
|
||||||
const baseUrl = "https://api.open-meteo.com/v1/forecast";
|
const baseUrl = "https://api.open-meteo.com/v1/forecast";
|
||||||
const params = ["latitude=" + lat, "longitude=" + lon, "hourly=weather_code,temperature_2m", "daily=weather_code,temperature_2m_max,temperature_2m_min,sunrise,sunset", "current=temperature_2m,relative_humidity_2m,apparent_temperature,is_day,weather_code,wind_speed_10m", "timezone=auto", "forecast_days=7"];
|
const params = ["latitude=" + lat, "longitude=" + lon, "hourly=weather_code,temperature_2m", "daily=weather_code,temperature_2m_max,temperature_2m_min,sunrise,sunset", "current=temperature_2m,relative_humidity_2m,apparent_temperature,is_day,weather_code,wind_speed_10m", "timezone=auto", "forecast_days=7"];
|
||||||
|
|
||||||
|
|
@ -192,6 +209,13 @@ Singleton {
|
||||||
|
|
||||||
onLocChanged: fetchWeatherData()
|
onLocChanged: fetchWeatherData()
|
||||||
|
|
||||||
|
Connections {
|
||||||
|
target: Config.services
|
||||||
|
function onWeatherLocationChanged(): void {
|
||||||
|
root.reload();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Refresh current location hourly
|
// Refresh current location hourly
|
||||||
Timer {
|
Timer {
|
||||||
interval: 3600000 // 1 hour
|
interval: 3600000 // 1 hour
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue