An Android library that lets you get the current weather at the user's location!
- Location is precise up to 7 decimal places (highest precision)
- No need to add any permissions in manifest manually
- No need to add google play services location lib in gradle manually
- Uses Google location services API internally - so you're in safe hands
- Tries to get location stored on phone if Google's API fails
- Connects automatically with OpenWeather to get users local weather
- Declare localWeather in your activity
- Override
onActivityResultand calllocalWeather.onActivityResultinside it if using current user location - Override
onRequestPermissionsResultand calllocalWeather.onRequestPermissionsResultinside it if using current user location
Example:
publicclassMainActivityextendsAppCompatActivity {
privateLocalWeatherlocalWeather;
@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
localWeather = newLocalWeather(this, "OpenWeatherApiKey");
localWeather.setUseCurrentLocation(true);
localWeather.setUpdateCurrentLocation(true);
localWeather.lang = Lang.ENGLISH;
localWeather.unit = Units.METRIC;
localWeather.setWeatherCallback(newLocalWeather.WeatherCallback(){
@OverridepublicvoidonSuccess(Weatherweather) { updateWeatherDetails(weather);
}
@OverridepublicvoidonFailure(Throwableexception) {
Log.e("Weather fetching", exception.getMessage());
}
});
localWeather.fetchCurrentLocation(newLocalWeather.CurrentLocationCallback(){
@OverridepublicvoidonSuccess(Locationlocation) {
localWeather.fetchCurrentWeatherByLocation(location);
}
@OverridepublicvoidonFailure(LocationFailedEnumfailed) {
Log.e("Location fetching", failed.toString());
}
});
}
privatevoidupdateWeatherDetails(Weatherweather) {
Stringunit = (localWeather.unit == Units.METRIC) ? "°C" : "°F" ;
exampleTextview.setText(((int) weather.getTemperature()) + unit); //Output: Temp with desired unit//do whatever you want
}
@OverrideprotectedvoidonActivityResult(intrequestCode, intresultCode, @NullableIntentdata) {
super.onActivityResult(requestCode, resultCode, data);
localWeather.onActivityResult(requestCode, resultCode, data);
}
@OverridepublicvoidonRequestPermissionsResult(intrequestCode, @NonNullString[] permissions, @NonNullint[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
localWeather.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}You can look at the code of the MainActivity to understand it better and to see some examples of what information you can retrieve with the Weather class
Add this line in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
...
}
}Add this line in your app build.gradle:
dependencies {
...
implementation 'com.github.interaapps:LocalWeather-Android:2.0'...
}If this library has helped you, please show your support by donating to this project:
Feel free to open a Pull Request
Thank you :)
