Skip to content

Instantly share code, notes, and snippets.

@jkreiser
Created August 14, 2014 03:12
Show Gist options
  • Save jkreiser/cde1c97eab54a4b9b64b to your computer and use it in GitHub Desktop.
Save jkreiser/cde1c97eab54a4b9b64b to your computer and use it in GitHub Desktop.
Android - LocationClient example
import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
public class LocationExampleActivity extends Activity
implements
ConnectionCallbacks,
OnConnectionFailedListener,
LocationListener {
private static final String TAG = LocationExampleActivity.class.getSimpleName();
public final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
private LocationClient locationClient;
private LocationRequest locationRequest;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locationClient = new LocationClient(this, this, this);
locationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(1000)
.setFastestInterval(1000);
}
@Override
public void onStart() {
super.onStart();
locationClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "locationClient connected");
if (locationClient != null) {
locationClient.requestLocationUpdates(locationRequest, this);
}
}
@Override
public void onDisconnected() {
Log.i(TAG, "locationClient disconnected");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(TAG, "locationClient connection failed");
if (connectionResult.hasResolution()) {
try {
connectionResult.startResolutionForResult(
this,
CONNECTION_FAILURE_RESOLUTION_REQUEST);
} catch (IntentSender.SendIntentException e) {
Log.e(TAG, "IntentSender.SendIntentException", e);
}
} else {
Log.e(TAG, "connectionResult has no resolution");
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case CONNECTION_FAILURE_RESOLUTION_REQUEST:
switch (resultCode) {
case Activity.RESULT_OK:
locationClient.connect();
break;
}
}
}
@Override
public void onStop() {
if (locationClient != null) {
if (locationClient.isConnected()) {
locationClient.removeLocationUpdates(this);
locationClient.disconnect();
}
}
super.onStop();
}
@Override
public void onLocationChanged(Location location) {
Log.i(TAG, "Location updated: " + location.getLatitude() + ", " + location.getLongitude());
// do something with the Location
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment