Location Services in Android (1) – Getting Last Known Location

For some reasons in your application, you might need to get some location-based details of your users to trigger some events. This is where Location API or better still, Google play location services API. Google location services API is part of the Google play services. The location APIs available in Google Play services facilitate adding location awareness to your app.
Some of the actions you can perform from the details you get from this service are:

  • Getting last known location
  • Getting location updates
  • Activity detection: This allows you to get and interpret the activities performed by your users at that moment e.g walking, standing or in transit.
  • Geocoding : This simply means interreting the details of a location so that your user can relate with it.
  • Geofencing: This allows you to set up a VIRTUAL FENCE around a particular location. One of the use cases is notifying your users of offers available in a coffee shop when they ENTER that VIRTUAL FENCE.
  • To use this API, devices must have Google play services installed.
    And you must also do the following:
    1. Include dependency in your app by adding compile 'com.google.android.gms:play-services-location:8.3.0' to your build.gradle (app) file.
    2. Add the following permission to your manifest file
    uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
    3. We need to CONNECT to GoogleAPIClient, which is the main entry point for Google Play services integration. Therefore, our activity must implement the interfaces listed below:

      GoogleApiClient.OnConnectionFailedListener: This implements a call back (onConnectionFailed()) that lets you know if the connection failed for whatever reason.
      GoogleApiClient.ConnectionCallbacks: This implements some call backs that lets you know the status of your connection (connected or suspended)

    The snippet below shows how to BUILD GoogleAPIClient

      GoogleApiClient mGac;
      void buildGoogleApiClient() {
      mGac = new GoogleApiClient.Builder(this)
      .addApi(LocationServices.API)//add this for location
      .addConnectionCallbacks(this)
      .addOnConnectionFailedListener(this)
      .build();
      }
      public void onConnected(Bundle bundle) {
      Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGac);
      if (mLastLocation != null) {
      lastLocation.setText("Location: " + mLastLocation.getLatitude() + "\nLongitude: " + mLastLocation.getLongitude());
      Log.d("lo","Location: " + mLastLocation.getLatitude() + "\nLongitude: " + mLastLocation.getLongitude());
      }
      }
      @Override
      public void onConnectionSuspended(int i) {
      Log.i(TAG,"Connection has been suspended);
      }
      @Override
      public void onConnectionFailed(ConnectionResult connectionResult) {
      Log.i(TAG,"Connection to google api client failed);
      }

    You can override onStart() and onStop() methods of your activity so you can CONNECT and DISCONNECT GoogleAPIClient. Before any operation is executed, the GoogleApiClient must be connected using the connect() method.

      @Override
      protected void onStop() {
      super.onStop();
      if (mGac.isConnected()) {
      mGac.disconnect();
      }
      }
      @Override
      protected void onStart() {
      super.onStart();
      mGac.connect();
      }

    You can find the whole project here on Github.