forked from hussien89aa/AndroidTutorialForBeginners
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
hussienalrubaye
committed
Oct 2, 2016
1 parent
6c838ee
commit 9377dbe
Showing
1 changed file
with
140 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
public class MyLocationListener | ||
implements GoogleApiClient.ConnectionCallbacks, | ||
GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener | ||
{ | ||
private final String TAG = "LOC_RECURRING_SAMPLE"; | ||
|
||
// Constants that define how often location updates will be delivered | ||
private final long LOC_UPDATE_INTERVAL = 10000; // 10s in milliseconds | ||
private final long LOC_FASTEST_UPDATE = 5000; // 5s in milliseconds | ||
|
||
protected GoogleApiClient mGoogleApiClient; | ||
protected LocationRequest mLocRequest; | ||
public Location mCurLocation; | ||
|
||
Context context; | ||
public MyLocationListener( Context context) { | ||
this.context=context; | ||
mCurLocation = null; | ||
|
||
// build the Play Services client object | ||
mGoogleApiClient = new GoogleApiClient.Builder(context) | ||
.addConnectionCallbacks(this) | ||
.addOnConnectionFailedListener(this) | ||
.addApi(LocationServices.API) | ||
.build(); | ||
mLocRequest=new LocationRequest(); | ||
mLocRequest.setInterval(LOC_UPDATE_INTERVAL); | ||
mLocRequest.setFastestInterval(LOC_FASTEST_UPDATE); | ||
mLocRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); | ||
// TODO: create the LocationRequest we'll use for location updates | ||
onStart(); | ||
} | ||
|
||
|
||
public void startLocationUpdates() { | ||
// TODO: start the location updates | ||
|
||
LocationServices.FusedLocationApi.requestLocationUpdates( | ||
mGoogleApiClient,mLocRequest,this); | ||
|
||
|
||
} | ||
|
||
public void stopLocationUpdates() { | ||
// TODO: stop the updates | ||
|
||
LocationServices.FusedLocationApi.removeLocationUpdates( | ||
mGoogleApiClient, this); | ||
|
||
|
||
} | ||
|
||
protected void updateUI() { | ||
// take the lat and long of the current location object and add it to the list | ||
if (mCurLocation != null) { | ||
String lat = String.format("Lat: %f\n", mCurLocation.getLatitude()); | ||
String lon = String.format("Lon: %f\n", mCurLocation.getLongitude()); | ||
|
||
|
||
} | ||
} | ||
|
||
protected void initializeUI() { | ||
// start by getting the last known location as a starting point | ||
if (mCurLocation == null) { | ||
mCurLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); | ||
|
||
updateUI(); | ||
|
||
|
||
} | ||
|
||
|
||
} | ||
|
||
/** | ||
* Called to handle the button clicks in the view | ||
*/ | ||
|
||
public void onClick(int id ) { | ||
switch (id){ | ||
case 1: | ||
startLocationUpdates(); | ||
break; | ||
case 0: | ||
stopLocationUpdates(); | ||
break; | ||
} | ||
} | ||
|
||
/** | ||
* Called by Play Services when the user's location changes | ||
*/ | ||
@Override | ||
public void onLocationChanged(Location loc) { | ||
mCurLocation = loc; | ||
LocationService.location=loc; | ||
updateUI(); | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
/** | ||
* Google Play Services Lifecycle methods | ||
*/ | ||
@Override | ||
public void onConnected(Bundle connectionHint) { | ||
|
||
initializeUI(); | ||
startLocationUpdates(); | ||
} | ||
|
||
@Override | ||
public void onConnectionFailed(ConnectionResult result) { | ||
Log.d(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode()); | ||
} | ||
|
||
@Override | ||
public void onConnectionSuspended(int cause) { | ||
Log.d(TAG, "Connection was suspended for some reason"); | ||
mGoogleApiClient.connect(); | ||
} | ||
|
||
/** | ||
* Activity lifecycle events | ||
*/ | ||
public void onStart() { | ||
|
||
mGoogleApiClient.connect(); | ||
} | ||
|
||
public void onStop() { | ||
mGoogleApiClient.disconnect(); | ||
|
||
} | ||
|
||
} |