Select Page

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.

1. Create an instance of the utility class and pass the Activity as a parameter.

   LocationHelper locationHelper;
   locationHelper=new LocationHelper(this);

2. For checking the permission during runtime, call the following method with the help of the above-created instance.


locationHelper.checkpermission();

3. At next implement the needed callbacks into the Activity.

public class MyLocationUsingHelper extends AppCompatActivity implements ConnectionCallbacks,
        OnConnectionFailedListener,OnRequestPermissionsResultCallback

4. 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);

}

5. 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();
}

6. 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();
}

user_location

7. 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.

View on GithubDownload .zip

Feel free to comment and share, keep watching this space get more updates on Android Stuff!

Elsewhere

Jaison Fernando

Android Developer at NFN Labs
Elsewhere