-1

I'm developing an android app and I want to retrieve user's current location as soon as the app is launched.

I'm using this code:

@Override
    public void onConnected(@Nullable Bundle bundle) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) {
            currentLatDouble = mLastLocation.getLatitude();
            currentLngDouble = mLastLocation.getLongitude();
        }
    }

I am using a Snackbar which pops up when the location is not detected and has a "RETRY" button. On pressing that "RETRY" button, the above code is executed again and this time the location is retrieved.

What I want is, I want to retrieve the location as soon as the app is launched.

Please let me know.

Hammad Nasir
  • 2,889
  • 7
  • 52
  • 133

1 Answers1

0

The LocationServices.FusedLocationApi.getLastLocation method may return null if there is no last known location yet.

The best use is to request a location update and handle the response in the onLocationChanged callback, like this:

@Override
public void onConnected(Bundle connectionHint) {
    ...
    if (mRequestingLocationUpdates) {
        startLocationUpdates();
    }
}

protected void startLocationUpdates() {
    LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, this);
}

And the the LocationListener will process the received location

public class MainActivity extends ActionBarActivity implements LocationListener {
...
@Override
public void onLocationChanged(Location location) {
    mCurrentLocation = location;
}

Be sure to check the documentations. This answer also may help you.

Community
  • 1
  • 1
Diego Malone
  • 1,044
  • 9
  • 25
  • I don't know why, but the location is not getting retrieved on the app launch and is getting retrieved on the second try! – Hammad Nasir Jul 14 '16 at 01:14
  • It is because when the app launches, the request to the last location is before it can get a location fix. In subsequents tries, it has enough time to get some fix. If you put the retry action right after the init, maybe in this second call, it will still don't have the position. The best way is to request a position and as soon as the device gets a position, you assume that as the first known position. – Diego Malone Jul 14 '16 at 23:49