Request your location updates using a GoogleApiClient e.g. like this and use Toasts to verify your implementation:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
float LOCATION_REFRESH_DISTANCE = 5000;
long LOCATION_REFRESH_TIME = 0;
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_REFRESH_TIME, LOCATION_REFRESH_DISTANCE, mlocationListener);
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if(mLastLocation!=null){
Toast.makeText(getApplicationContext(), "YES! mLastLocation!=null", Toast.LENGTH_SHORT).show();
double latitude = mLastLocation.getLatitude();
double longitude = mLastLocation.getLongitude();
Toast.makeText(getApplicationContext(), "Latitude = " + latitude + "\nLongitude = " + longitude, Toast.LENGTH_SHORT).show();
}
}
Here you would be able to parametrize your Location requests with the variables LOCATION_REFRESH_DISTANCE
and LOCATION_REFRESH_TIME
(in milliseconds)
You would need of course the LocationListener object mLocationListener
that your GoogleApiClient mGoogleApiClient
needs to get the last known / current location and a Location object (I called it mLastLocation
) where to store it. eg.:
private Location mLastLocation;
LocationListener mlocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
mLastLocation=location;
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};