Why is FusedLocationApi.getLastLocation null Why is FusedLocationApi.getLastLocation null android android

Why is FusedLocationApi.getLastLocation null


The fused location provider will only maintain background location if at least one client is connected to it. Now just turning on the location service will not guarantee storing the last known location.

Once the first client connects, it will immediately try to get a location. If your activity is the first client to connect and getLastLocation() is invoked right away in onConnected(), that might not be enough time for the first location to arrive..

Then you are setting mLocationRequest.setFastestInterval(30000); which basically means 30 seconds. so at least 30 seconds after and generally according to your setting preferred time is 120 secs, isn't it a very long time if there is no stored last known location at all? plus you are setting battery balanced priority, which will make you waiting longer time.

mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

I suggest you to launch Maps app first, so that there is at least some confirmed location and then test your app.


I am using FusedLocationProvider api in my app,Here is my code that works both on device and emulator -

  @Override  protected void onCreate(Bundle savedInstanceState){     //put your code here     ....     getLocation();  } private void getLocation(){    locationRequest = LocationRequest.create();    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);    locationRequest.setInterval(LOCATION_INTERVAL);    locationRequest.setFastestInterval(LOCATION_INTERVAL);    fusedLocationProviderApi = LocationServices.FusedLocationApi;    googleApiClient = new GoogleApiClient.Builder(this)    .addApi(LocationServices.API)    .addConnectionCallbacks(this)    .addOnConnectionFailedListener(this)    .build();    if (googleApiClient != null) {        googleApiClient.connect();    }}   @Override   public void onConnected(Bundle arg0) {        fusedLocationProviderApi.requestLocationUpdates(googleApiClient,  locationRequest, this);   }   @Override   public void onLocationChanged(Location location) {        Toast.makeText(mContext, "location :"+location.getLatitude()+" , "+location.getLongitude(), Toast.LENGTH_SHORT).show();    }

this is working code of mine.

Hope my code help you.

Cheers..


If it returns null, this means you need start receiving location updates with LocationServices.FusedLocationApi.requestLocationUpdates before retrieving the location.

For me, a solution to the problem was to call the:

LocationServices.FusedLocationApi.requestLocationUpdates(locationApiClient,locationRequest, this);

before the:

App.setLocation(LocationServices.FusedLocationApi.getLastLocation(locationApiClient));

The problem is that the last location does not exist so it was null.

If the problem persist try this:

Check the necessary permissions.

Ensure you've enabled position on your device, then restart.