WeatherRequest.
WeatherRequest req = new WeatherRequest(40.716667F, -74F);
WeatherRequest req = new WeatherRequest(2643743);
Openweathermap | Yes |
Weatherunderground Weather.com |
Yes |
Yahoo! Weather | Yes |
Forecast.io | Yes |
weatherClient.searchCity(s.toString(), new WeatherClient.CityEventListener() {
@Override
public void onCityListRetrieved(Listcities) {
// The data is ready
}
@Override
public void onWeatherError(WeatherLibException e) {
// Error on geting data
}
@Override
public void onConnectionError(Throwable throwable) {
// Connection error
}
});
where weatherClient is created in the way shown before
As you can notice, we can search a city passing a name pattern and
invoking the method
searchCity
. All the methods in the libray have a listener
that is used to notify the
caller that the data from weather provider are ready. In this case we
have to use CityEventListener
that has three methods:
onCityListRetrieved
that is called when the data is
availableonWeatherError
that is called when there is an error
parsing the dataonConnectionError
that is called when there is a
connection error
The most interesting method is the first one that returns a List of
City instance. One attribute
of this class is the id
that is the unique city
indentifier. The City
holds
other attributes like the region name and the country. It holds also
the long and lat.
The onCityListRetrieved
method is the right place if you
want to create an adapter to fill a ListView for example so that the
user can select the city.
There are other two different ways to search for a city id:
If you want to search using lat and long coords, you have to use:searchCity(double lat, double lon, CityEventListener listener)
Criteria
you should provide your search location criteria (see Android docs).
Weatherlib provides a default Criteria implementation:
public static Criteria createDefaultCriteria() {
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setCostAllowed(false);
}