add find weather location by postcode

todo: add country


Origin commit data
------------------
Commit: b2b2d605dc
Author: TangoCash <eric@loxat.de>
Date: 2022-01-20 (Thu, 20 Jan 2022)
This commit is contained in:
TangoCash
2022-01-20 21:24:37 +01:00
committed by vanhofen
parent 42179b9b7e
commit ab7fc894a1
8 changed files with 70 additions and 0 deletions

View File

@@ -182,6 +182,42 @@ bool CWeather::GetWeatherDetails()
return false;
}
bool CWeather::FindCoords(int postcode, std::string country)
{
std::string data = "http://api.openweathermap.org/geo/1.0/zip?zip=" + std::to_string(postcode) + ","+country+"&appid=" + key;
JSONCPP_STRING answer;
JSONCPP_STRING formattedErrors;
Json::CharReaderBuilder builder;
Json::CharReader *reader = builder.newCharReader();
Json::Value DataValues;
answer.clear();
if (!getUrl(data, answer))
{
delete reader;
return false;
}
bool parsedSuccess = reader->parse(answer.c_str(), answer.c_str() + answer.size(), &DataValues, &formattedErrors);
delete reader;
if (!parsedSuccess)
{
printf("Failed to parse JSON\n");
printf("%s\n", formattedErrors.c_str());
return false;
}
if (DataValues["message"].asString() == "not found")
return false;
float lat = DataValues["lat"].asFloat();
float lon = DataValues["lon"].asFloat();
g_settings.weather_city = DataValues["name"].asString();
g_settings.weather_location = std::to_string(lat) + "," + std::to_string(lon);
return true;
}
void CWeather::show(int x, int y)
{
checkUpdate();