Get lattitude and longitude from google fused Api in just few lines of code. It Checks for Error and Exceptions, Play services, GPS, Network, permission, etc.
-
Reduces Code Boilerplate and saves development time.
-
Very light size < 10kb.
-
Get Current Location.
-
Get Periodic Location.
-
Checks for Play Services.
-
Checks for location on/off.
-
In-app GPS enable dialog.
-
Inbuilt Location Permission Request.
-
Single errorCallback for most of the errors.
-
Can be used with any context providers like Activity, Services, Fragment, AppCompatActivity, Dialogs, etc.
-
Get Address of a location.
For getting current location one time:
MbLocationServices mbLocationServices = MbLocationServices.with(this);
// Optional Params
mbLocationServices.setFastestInterval(1000 * 5);
mbLocationServices.setInterval(1000 * 10);
mbLocationServices.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mbLocationServices.setOneFix(true);
mbLocationServices.init(new MbLocationListener() {
@Override
public void onLocationUpdate(Location location) {
Toast.makeText(MainActivity.this, "Latitude=" + location.getLatitude() + ", Longitude=" + location.getLongitude(), Toast.LENGTH_LONG).show();
}
@Override
public void onError(MbLocationError errorCode) {
Toast.makeText(MainActivity.this, "Error=" + errorCode.message, Toast.LENGTH_LONG).show();
switch (errorCode.errorCode) {
case MbLocationUtil.LOCATION_PLAY_SERVICE_ERROR:
// Do your project specific stuff here..
break;
case MbLocationUtil.LOCATION_PROVIDER_ERROR:
// Do your project specific stuff here..
break;
}
}
});
For getting current location (Periodic):
mbLocationServices.setOneFix(false);
For stopping the location just use the stop method.
MbLocationServices.with(this).stopLocationUpdates();
// To get the human readable address
Address address = MbLocationUtil.with(mContext).getAddressFromLocation(location.getLatitude(), location.getLongitude());
// Check if the location services are enabled
MbLocationUtil.with(mContext).locationServicesEnabled();
// Check if any provider (network or gps) is enabled
MbLocationUtil.with(mContext).isAnyProviderAvailable();
// Check if GPS is available
MbLocationUtil.with(mContext).isGpsAvailable();
// Check if Network is available
MbLocationUtil.with(mContext).isNetworkAvailable();
// Check if the passive provider is available
MbLocationUtil.with(mContext).isPassiveAvailable();
// Check if the location is mocked
MbLocationUtil.with(mContext).isMockSettingEnabled();