Created
November 11, 2014 11:03
-
-
Save iraycd/7e4016fce70d4deb02cd to your computer and use it in GitHub Desktop.
Android Location with GPS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.location.model; | |
public class LatLong { | |
private double lat, lng; | |
public LatLong() { | |
} | |
public double getLat() { | |
return lat; | |
} | |
public void setLat(double lat) { | |
this.lat = lat; | |
} | |
public double getLng() { | |
return lng; | |
} | |
public void setLng(double lng) { | |
this.lng = lng; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.location.helper; | |
import android.app.Dialog; | |
import android.content.IntentSender; | |
import android.location.Location; | |
import android.os.Bundle; | |
import android.support.v4.app.DialogFragment; | |
import android.support.v4.app.FragmentActivity; | |
import com.google.android.gms.common.ConnectionResult; | |
import com.google.android.gms.common.GooglePlayServicesClient; | |
import com.google.android.gms.common.GooglePlayServicesUtil; | |
import com.google.android.gms.location.LocationClient; | |
import com.google.android.gms.location.LocationListener; | |
import com.google.android.gms.location.LocationRequest; | |
import com.location.model.LatLong; | |
public class LocationHelper implements LocationListener, | |
GooglePlayServicesClient.ConnectionCallbacks, | |
GooglePlayServicesClient.OnConnectionFailedListener { | |
public interface OnLocationReceived { | |
public void onLocationReceived(LatLong latlong); | |
} | |
private LocationRequest mLocationRequest; | |
private LocationClient mLocationClient; | |
private OnLocationReceived mLocationReceived; | |
private FragmentActivity context; | |
private LatLong latLong; | |
public LocationHelper(FragmentActivity context) { | |
this.context = context; | |
mLocationRequest = LocationRequest.create(); | |
/* | |
* Set the update interval | |
*/ | |
mLocationRequest | |
.setInterval(LocationUtils.UPDATE_INTERVAL_IN_MILLISECONDS); | |
// Use high accuracy | |
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); | |
// Set the interval ceiling to one minute | |
mLocationRequest | |
.setFastestInterval(LocationUtils.FAST_INTERVAL_CEILING_IN_MILLISECONDS); | |
mLocationClient = new LocationClient(context, this, this); | |
} | |
public void setLocationReceivedLister(OnLocationReceived mLocationReceived) { | |
this.mLocationReceived = mLocationReceived; | |
} | |
public LatLong getCurrentLocation() { | |
return latLong; | |
} | |
public LatLong getLastLocation() { | |
// If Google Play Services is available | |
if (servicesConnected()) { | |
// Get the current location | |
Location currentLocation = mLocationClient.getLastLocation(); | |
// Display the current location in the UI | |
latLong = LocationUtils.getLatLng(currentLocation); | |
} | |
return latLong; | |
} | |
public void onStart() { | |
mLocationClient.connect(); | |
} | |
public void onStop() { | |
// If the client is connected | |
if (mLocationClient.isConnected()) { | |
stopPeriodicUpdates(); | |
} | |
// After disconnect() is called, the client is considered "dead". | |
mLocationClient.disconnect(); | |
} | |
private boolean servicesConnected() { | |
// Check that Google Play services is available | |
int resultCode = GooglePlayServicesUtil | |
.isGooglePlayServicesAvailable(context); | |
// If Google Play services is available | |
if (ConnectionResult.SUCCESS == resultCode) { | |
// Continue | |
return true; | |
// Google Play services was not available for some reason | |
} else { | |
// Display an error dialog | |
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, | |
context, 0); | |
if (dialog != null) { | |
ErrorDialogFragment errorFragment = new ErrorDialogFragment(); | |
errorFragment.setDialog(dialog); | |
errorFragment.show(context.getSupportFragmentManager(), | |
LocationUtils.APPTAG); | |
} | |
return false; | |
} | |
} | |
@Override | |
public void onLocationChanged(Location location) { | |
latLong = LocationUtils.getLatLng(location); | |
if (mLocationReceived != null && latLong != null) { | |
mLocationReceived.onLocationReceived(latLong); | |
} | |
} | |
@Override | |
public void onConnectionFailed(ConnectionResult connectionResult) { | |
if (connectionResult.hasResolution()) { | |
try { | |
// Start an Activity that tries to resolve the error | |
connectionResult.startResolutionForResult(context, | |
LocationUtils.CONNECTION_FAILURE_RESOLUTION_REQUEST); | |
/* | |
* Thrown if Google Play services canceled the original | |
* PendingIntent | |
*/ | |
} catch (IntentSender.SendIntentException e) { | |
// Log the error | |
e.printStackTrace(); | |
} | |
} else { | |
// If no resolution is available, display a dialog to the user with | |
// the error. | |
showErrorDialog(connectionResult.getErrorCode()); | |
} | |
} | |
@Override | |
public void onConnected(Bundle connectionHint) { | |
startPeriodicUpdates(); | |
} | |
@Override | |
public void onDisconnected() { | |
} | |
private void startPeriodicUpdates() { | |
mLocationClient.requestLocationUpdates(mLocationRequest, this); | |
} | |
/** | |
* In response to a request to stop updates, send a request to Location | |
* Services | |
*/ | |
private void stopPeriodicUpdates() { | |
mLocationClient.removeLocationUpdates(this); | |
} | |
public void showErrorDialog(int errorCode) { | |
// Get the error dialog from Google Play services | |
Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(errorCode, | |
context, LocationUtils.CONNECTION_FAILURE_RESOLUTION_REQUEST); | |
// If Google Play services can provide an error dialog | |
if (errorDialog != null) { | |
// Create a new DialogFragment in which to show the error dialog | |
ErrorDialogFragment errorFragment = new ErrorDialogFragment(); | |
// Set the dialog in the DialogFragment | |
errorFragment.setDialog(errorDialog); | |
// Show the error dialog in the DialogFragment | |
errorFragment.show(context.getSupportFragmentManager(), | |
LocationUtils.APPTAG); | |
} | |
} | |
public static class ErrorDialogFragment extends DialogFragment { | |
// Global field to contain the error dialog | |
private Dialog mDialog; | |
/** | |
* Default constructor. Sets the dialog field to null | |
*/ | |
public ErrorDialogFragment() { | |
super(); | |
mDialog = null; | |
} | |
/** | |
* Set the dialog to display | |
* | |
* @param dialog | |
* An error dialog | |
*/ | |
public void setDialog(Dialog dialog) { | |
mDialog = dialog; | |
} | |
/* | |
* This method must return a Dialog to the DialogFragment. | |
*/ | |
@Override | |
public Dialog onCreateDialog(Bundle savedInstanceState) { | |
return mDialog; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment