1

I've read some questions about this, but I didn't quite find an answer that I needed. I want to understand how getLastLocation works internally. How last known location cached value is updated?

We have requirement where we need to get last known location of the device. Can anyone explain how getLastLocation works? I have seen that getLastLocation API returns null even if location-setting is ON. At the same time if I checked Google Map app and it is able to get the location (of-course it must be using requestLocationUpdates or any other API).

On my Nexus-5, I observed that getLastLocation returns null even after I launch Google Map app and then launch my app again. Even if location is detected correctly in Google Map app, getLastLocation API returns null.

How Android system updates the last known location? Does it update only when some app requests for location via requestLocationUpdates API? If Android system updates the last known location automatically then why do getLastLocation API returns null?

getLastLocation API Getting last known location

Points to be noted:

  1. Location permission is granted.

  2. Location Setting is ON

  3. Same code works on few devices, but fails on few devices.

  4. I am trying to get location from Google's FusedLocationProviderAPI and Android Framework LocationManager API.

  5. I have checked all existing questions on Stackoverflow

It would be great if anyone can explain under what circumstances getLastLocation API can return null even if location setting is ON. Does anyone know how this works internally?

srs
  • 647
  • 3
  • 11
  • Can you share some code? – S Haque Apr 18 '17 at 05:10
  • Check this answer http://stackoverflow.com/questions/20438627/getlastknownlocation-returns-null – Faraz Apr 18 '17 at 05:32
  • This question is duplicate please check http://stackoverflow.com/questions/16830047/locationclient-getlastlocation-return-null – Rajesh N Apr 18 '17 at 05:33
  • have you got the location permissions? – Vladyslav Matviienko Apr 18 '17 at 05:46
  • Edited the question. Here, the issue is not with the code. I am interested to know how getLastLocation works internally. How the behavior differs device to device? – srs Apr 18 '17 at 05:55
  • Have you seen [this](http://stackoverflow.com/questions/20438627/getlastknownlocation-returns-null) question? – Qandeel Abbassi Apr 18 '17 at 05:58
  • And make sure your googleapiclient is connected by using connection callbacks – Qandeel Abbassi Apr 18 '17 at 06:03
  • Yes I have seen the mentioned questions. There is no problem with code because it works sometimes. I have ensured that googleapiclient is connected. Here, I am unable to understand why getLastLocation API returns null even if location is ON. How Android system manages this cached location? Does it listens to location automatically OR do we have to forcefully call requestLocationUpdate to have cached value in getLastLocation API? – srs Apr 18 '17 at 06:05

1 Answers1

0

getLastLocation : This method will give you the last location available on your device.

In some cases if your device don't have any last location then it will return null.

You should use onLocationChanged method for current location.

Please see example here you will get it https://developer.android.com/training/location/receive-location-updates.html

According to doc

public abstract Location getLastLocation (GoogleApiClient client)

Returns the best most recent location currently available.

If a location is not available, which should happen very rarely, null will be returned. The best accuracy available while respecting the location permissions will be returned.

This method provides a simplified way to get location. It is particularly well suited for applications that do not require an accurate location and that do not want to maintain extra logic for location updates.

Hope these help you.

Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
  • Yes I agree that I can listen to location updates. That is available solution but I am curious to understand why getLastLocation returns null even if location setting is ON. Do you know how it works internally? – srs Apr 18 '17 at 06:03
  • as I explained above if your device don't have any last location then it will return null. Last location only gets when your device previously hit for location. – Jitesh Mohite Apr 18 '17 at 06:06
  • Right, so my location setting is ON, network is available. All conditions are satisfied. I am moving. Still getLastLocation returns null. I am unable to understand how it is implemented internally. Does Android system automatically listens for location-change to update this last known cached value? – srs Apr 18 '17 at 06:08
  • "In some cases if your device don't have any last location then it will return null.": What are these some cases? Do you know under what circumstances getLastLocation gives you null even if setup is correct? – srs Apr 18 '17 at 06:09
  • On my Nexus-5, I observed that getLastLocation returns null even after I launch Google Map app and then launch my app again. Even if location is detected correctly in Google Map app, getLastLocation API returns null. – srs Apr 18 '17 at 06:13