Weather library

Weatherlib
Search city




Introduction

Once you created the weather client you are ready to get weather information. You have different ways to
get weather information:
  • By city id
  • By latitude and longitude
The city id is the unique city identification used by the weather provider. Each weather provider has its city
identification, while if you use the coords they are the same.
When you use the city id you have to look for it using the search method. WeatherLib supports also the city search and we will discover it later.
All the methods used to get weather information (current, forecast and so on), accept WeatherRequest.
This class has two constructor: one that accepts the city id and the other one that accepts the lat and long.
The library will create the right URL depending on the parameter you used (city id or coords).
Let make an example, suppose we want to get the weather information for NY using the coords, we can create the request in this way:
WeatherRequest req = new WeatherRequest(40.716667F, -74F);

if we want to get the weather information in London, UK we can use:
WeatherRequest req = new WeatherRequest(2643743);

The Search city features is supported by:

Openweathermap Yes
Weatherunderground
Weather.com
Yes
Yahoo! Weather Yes
Forecast.io Yes

Search for a city

Usually in a weather app, one of its feature is letting the user to search for the city. Weatherlib helps you searching the city passing a city pattern name:
weatherClient.searchCity(s.toString(), new WeatherClient.CityEventListener() {
@Override
public void onCityListRetrieved(List cities) {
// 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 available
  • onWeatherError that is called when there is an error parsing the data
  • onConnectionError 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:

  • Using latitude and logitude
  • Using Android Criteria
If you want to search using lat and long coords, you have to use:

searchCity(double lat, double lon, CityEventListener listener)

If you want to use 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);
}