Skip to content Skip to sidebar Skip to footer

Getlastlocation Returns A Null Value

I have followed this guide https://developer.android.com/training/location/retrieve-current.html#permissions, and I'm not able to receive the last location. I need the location ju

Solution 1:

getLastLocation() has a high tendency to return null. It also does not request a new location, so even if you get a location, it could be very old, and not reflect the current location. Better to register a listener, even if you just unregister after you get the first onLocationChanged() callback.

This question gets asked a lot, and usually is marked as a duplicate of questions like this one

However, in your case, it also looks like you just forgot to call connect():

buildGoogleApiClient();
 mGoogleApiClient.connect(); //added

You can use the code in this answer as a reference for registering for location callbacks, which is suggested if you want to get an accurate current location.

Edit: Since you need only one location, here is a slightly modified version of that code, which requests location updates, and then un-registers for location updates after the first location comes in.

publicclassMainActivityextendsActivityimplementsGoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, LocationListener {

    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    privateLocation mLastLocation;
    private double mLatitude;
    private double mLongitude;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buildGoogleApiClient();
        mGoogleApiClient.connect();
    }

    @OverrideprotectedvoidonPause(){
        super.onPause();
        if (mGoogleApiClient != null) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        }
    }

    protected synchronized voidbuildGoogleApiClient() {

        mGoogleApiClient = newGoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    @OverridepublicvoidonConnected(Bundle bundle) {

        mLocationRequest = newLocationRequest();
        mLocationRequest.setInterval(1000);
        mLocationRequest.setFastestInterval(1000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

        //mLocationRequest.setSmallestDisplacement(0.1F);LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }

    @OverridepublicvoidonConnectionSuspended(int i) {
        Toast.makeText(this,"onConnectionSuspended",Toast.LENGTH_SHORT).show();
    }

    @OverridepublicvoidonConnectionFailed(ConnectionResult connectionResult) {
        Toast.makeText(this,"onConnectionFailed",Toast.LENGTH_SHORT).show();
    }

    @OverridepublicvoidonLocationChanged(Location location) {
        mLastLocation = location;
        //no need to do a null check here:
        mLatitude = location.getLatitude();
        mLongitude = location.getLongitude();

        //remove location updates if you just need one location:if (mGoogleApiClient != null) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        }
    }
}

Post a Comment for "Getlastlocation Returns A Null Value"