Android provides several location APIs that you can use to build applications that mainly focus on location. In this section, we are going to understand how to use Location Services in your Application.
To get the current location on your application, you need to create a location client with the help of the LocationClient object. The location client needs to be connected to location services with the help of connect() method and then it is called using the getLastLocation() method.
There are two interfaces that you need to implement the location functionality in your application:
- GooglePlayServicesClient.OnConnectionFailedListener
- GooglePlayServicesClient.ConnectionCallbacks
These two interfaces provide some important callbacks method that includes:
- abstract void onDisconnected(): This method is called after it gets disconnected from the client. To disconnect from the location client, the disconnect() method is used.
- abstract void onConnected(Bundle connectionHint): This callback method is called after the successful connection of the location client with the location service.
- abstract void onConnectionFailed(ConnectionResult result): It is called when there occurs an error while connecting the client to the location service.