AwareWhen app requires knowledge about user location and places around him you don't want to struggle with details of the underlying location technology. In this talk, you will learn how to make your life easier with the new Fused Location Provider API.
7. • Add play services
dependencies {
compile
}
• Specify permissions
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
'com.google.android.gms:play-services-location:7.3.0'
Include in project
'com.google.android.gms:play-services:7.3.0'
8. Google API Client
• Connection to play services
googleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(playServicesConnectionCallback)
.addOnConnectionFailedListener(playServicesFailListener)
.addApi(LocationServices.API)
.build();
• Important
Because it is hard to anticipate the state of each device, you must always check
for a compatible Google Play services APK before you access Google Play
services features.
9. Connect to API
@Override
protected void onStart() {
super.onStart();
if (!resolvingError) {
googleApiClient.connect();
}
}
@Override
protected void onStop() {
googleApiClient.disconnect();
super.onStop();
}
11. Get location
• On client connected
private GoogleApiClient.ConnectionCallbacks playServicesConnectionCallback
= new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
if (lastLocation != null) {
lastLocationLabel.setText("Last known location: " +
lastLocation.getLatitude() + ", "
+ lastLocation.getLongitude());
}
}
@Override
public void onConnectionSuspended(int i) {
// The connection has been interrupted.
// Disable any UI components that depend on Google APIs
// until onConnected() is called.
}
};