North Border Software




Home Tutorials Hints & Tips About
Share on Facebook Share on Twitter

Android Location Based Services using the GoogleAPIClient and the FusedLocationProvider API - Part 2

In this second of this three-part tutorial set, we will create a framework of essential methods in the MainActivity.java file. First of all, however, we need to set up objects for the two TextViews created in the layout.

Below the MainActivity Class declaration and before the onCreate method, two TextView objects are declared.

TextView latitudeText;
TextView longitudeText;

In the onCreate method, these are linked to the TextViews in the layout:

latitudeText = (TextView) findViewById(R.id.tvLatitude);
longitudeText = (TextView) findViewById(R.id.tvLongitude);

We then need to set up two further objects:

private GoogleApiClient googleApiClient;
private LocationRequest locationRequest;

They are private as they are only to be used in this class. We are using Google Play Services to obtain our location data and quite simply, the GoogleAPIClient is the main entry point for Google Play services integration. Android Studio should automatically make the relevant imports.

We plan to use the FusedLocationProvider API. At this stage we need to create an object of the FusedLocationProviderApi. The FusedLocationProviderApi is the main entry point for interacting with the fused location provider. The code for this object is below:

private FusedLocationProviderApi locationProvider = LocationServices.FusedLocationApi;

The LocationRequest object is used to create quality of service parameters for use with the FusedLocationProvoder API. This will become clear when we look at the code associated with this object.

We now need to create a builder for the both the GoogleAPIClient and the LocationRequest objects.

The following code is needed for the GoogleApiClient:

googleApiClient = new GoogleApiClient.Builder(this)
     .addApi(LocationServices.API)
     .addConnectionCallbacks(this)
     .addOnConnectionFailedListener(this)
.build();

The LocationServices API is the main entry point for location services integration when Google Play Services are used. We then need to add essential call-back methods and methods for when a connection fails. You will see errors associated with this block of code. Android Studio can correct these errors. Essentially we need to let the Class implement these methods and add the unimplemented methods.

Two override methods are associated with the connection call-backs and they should be implemented. These are:

@Override
public void onConnected(Bundle bundle) {}

@Override
public void onConnectionSuspended(int i) {}

One override method is associated with the onConnectionFailed listener. This is:

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {}

As mentioned earlier, the LocationRequest object is used to create quality of service parameters for use with the FusedLocationProvoder API. The code we will use is below:

locationRequest = new LocationRequest();
locationRequest.setInterval(60 * 1000);
locationRequest.setFastestInterval(15 * 1000);
locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

As discussed in part 1, location based services consume battery power. Therefore, we will want to set a an update rate sufficient enough the meet the needs of the app, but at the same time, is not too frequent that we drain the battery unnecessarily. We do this with the line is code below:

locationRequest.setInterval(60 * 1000);

The value is in milliseconds, so here I have set it to 1 minute. There may be a situation where another app is generating location updates. In this case we can set a fastest interval with the line below on the premise that no extra power will be consumed if we use this location. I have set this to 15 seconds using the line is code below:

locationRequest.setFastestInterval(15 * 1000);

It is recommended that you set a value for fastest updates. Otherwise, one will be selected for you.

The next line of code sets the accuracy of the location. The more accurate the location the more power is consumed. Again, you must choose the best fit for your application. I have chosen to use LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY. This uses the coarse location such as triangulation from WiFi and phone masts and will be accurate to within 100m. Other options are:

PRIORITY_HIGH_ACCURACY. This returns the highest accuracy possible.

PRIORITY_LOW_POWER. This is city level accuracy within 10Km.

PRIORITY_NO_POWER. The app will listen passively for location updates from another app. No location is returned if there are none.

We need to implement one more method to complete our framework class. We need to implement the LocationListener and then implement the following override method:

@Override
public void onLocationChanged(Location location) {}

Quite simply we need to do something when our location changes.

Our basic framework is now complete. In part 3 we will complete the java coding and test the app.

After this tutorial you MainActivity.java file should look similar to the one below:


Download Download Location Project Files



© 2015 North Border Software All rights reserved
Privacy and Cookies Terms of Use Disclaimer Copyright
North Border Software Logo

Android Apps and Training