I am having an issue with an Android app I am developing. The application runs a countdown timer and sends a text message when the timer reaches zero. At 9 seconds, The application is supposed to get the location of the phone. When the timer reaches zero, it sends the location (received at 9 seconds) as a maplink in the text message.
The application works, but the GPS location it sends is sometimes over 100 meters away. I need the application to be as precise as possible. I have a version using the android.location framework and a version using the fusedlocationapi. The android.location version I have is far more accurate than the fusedlocationapi which is very confusing to me.
The thing that really bothers me is the following: If I run the timer and get an inaccurate location, I can open google maps (which will show a very accurate location), go back to my app, run the timer again and I will receive a precise location (similar to what google maps just showed me). Is google maps using a better method to get my location or is my code just bad? I thought the app would show the same location as google maps but it is not and this is frustrating me a great deal. Any thoughts on this would be greatly appreciated.
Here is my code where I add the fusedlocationapi:
/**
* Creating google api client object
* */
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
mGoogleApiClient.connect();
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest);
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult locationSettingsResult) {
final Status status = locationSettingsResult.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location requests here
getLocation();
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(SafetyAlarmActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
break;
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
switch (requestCode) {
case REQUEST_CHECK_SETTINGS:
switch (resultCode) {
case Activity.RESULT_OK:
// All required changes were successfully made
getLocation();
break;
case Activity.RESULT_CANCELED:
// The user was asked to change settings, but chose not to
break;
default:
break;
}
break;
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
Log.e(TAG, " mGoogleApiClient Connection connected:");
getLocation();
}
@Override
public void onConnectionSuspended(int i) {
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.e(TAG, "Connection failed: ConnectionResult.getErrorCode() = "+ connectionResult.getErrorCode());
}
Here is the code where the app gets the location at 9 seconds:
/*
update GPS location
*/
private void updateGPSLocation(long millisUntilFinished) {
Log.e("current time remaining", " time: "+millisUntilFinished/1000);
if(millisUntilFinished/1000==9)
{
Log.e("9 seconds remaining", "get locaiton");
getLocation();
}
}
/*
get location
*/
private void getLocation()
{
try
{
Location lastLocation = LocationServices.FusedLocationApi
.getLastLocation(mGoogleApiClient);
PersistObject.apply(con,AppConstant.LOCATION,lastLocation);
}
catch (SecurityException e)
{
e.printStackTrace();
}
}
Here are my permissions:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />