Knowing a user’s location in an Android app can be extremely useful. Users take their devices everywhere and are constantly using them on the go, and as developers, we can capitalise on that by providing a more contextual experience based on their current location.
My previous post explains, how to get users current location using FusedLocationProviderApi with step by step guide. In this part, we’ll discuss how to simplify the process with the help of LocationHelper.
LocationHelper is nothing but a util class which is taken care of finding users location. Using this we can get the current location within 5 minutes and also it will do the runtime permission check.
How to use LocationHelper?
LocationHelper implementation process is so simple.
Create an instance of the utility class and pass the Activity as a parameter.
LocationHelper locationHelper; | |
locationHelper=new LocationHelper(this); |
For checking the permission during runtime, call the following method with the help of the above-created instance.
locationHelper.checkpermission(); |
At next implement the needed callbacks into the Activity.
public class MyLocationUsingHelper extends AppCompatActivity implements ConnectionCallbacks, | |
OnConnectionFailedListener,OnRequestPermissionsResultCallback |
Redirect the implemented methods to the util function, then only it will automatically do the needed background works.
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
locationHelper.onActivityResult(requestCode,resultCode,data); | |
} | |
// Permission check functions | |
@Override | |
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, | |
@NonNull int[] grantResults) { | |
// redirects to utils | |
locationHelper.onRequestPermissionsResult(requestCode,permissions,grantResults) | |
} |
Then handle the ConnectionCallback implemented methods using the following snippet.
/** | |
* Google connection callback methods | |
*/ | |
@Override | |
public void onConnectionFailed(ConnectionResult result) { | |
Log.i("Connection failed:", "" + result.getErrorCode()); | |
} | |
@Override | |
public void onConnected(Bundle arg0) { | |
// Once connected with google api, get the location | |
mLastLocation=locationHelper.getLocation(); | |
} | |
@Override | |
public void onConnectionSuspended(int arg0) { | |
locationHelper.connectApiClient(); | |
} |
To check the availability of Google play service, use checkPlayServices() method. If it’s available then build the GoogleAPiClient using the below code.
// check availability of play services | |
if (locationHelper.checkPlayServices()) | |
{ | |
// Building the GoogleApi client | |
locationHelper.buildGoogleApiClient(); | |
} |
Once all the above steps are done, then we can easily get the location and also the address using the simple function call.
Location mLastLocation; | |
mLastLocation=locationHelper.getLocation(); | |
if (mLastLocation != null) { | |
latitude = mLastLocation.getLatitude(); | |
longitude = mLastLocation.getLongitude(); | |
Address locationAddress; | |
locationAddress=locationHelper.getAddress(latitude,longitude); | |
} |
That’s it. Now you have the current location and also the address. Happy coding 🙂
I created a sample project on GitHub to demonstrate the things covered in this article.
Feel free to comment and share, keep watching this space get more updates on Android Stuff!
Jaison Fernando
Latest posts by Jaison Fernando (see all)
- Phone number auth using Firebase Authentication SDK - March 20, 2020
- Password-less email auth using Firebase Authentication SDK - March 9, 2020
- How to use SharedPreferences API in Android? - February 10, 2020