flypen
flypen

Reputation: 2585

How to get location that isn't out of date?

After GPS is enabled, it will take some time before the GPS is ready. So use this code may get a location that is out of date: location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); I can't use the location that is out of date. How can I get the location that I need?

Here is a similar question: Android: getLastKnownLocation out-of-date - how to force location refresh?

From it, I can use LocationListener to get the updated location, which should be not out of date. It's correct. But how can I handle this case:

I have already enabled GPS, and then start my app. If I stay at one place and don't move, then onLocationChanged() won't be called. So I can't use this method to judge whether the return value of getLastKnownLocation() is not out-of-date?

Any ideas?

Upvotes: 1

Views: 1711

Answers (2)

skynet
skynet

Reputation: 9908

Each Location object has a getTime() method, which returns the time of the fix. You could use that to determine how old the fix is.

Upvotes: 1

Reno
Reno

Reputation: 33792

How to get location that isn't out of date?

You call requestLocationUpdates() with the minTime of 1000 ms, for every Location in onLocationChanged(), you call getAccuracy(). If the accuracy is acceptable for you, call removeUpdates().

So I can't use this method to judge whether the return value of getLastKnownLocation() is not out-of-date

I think the Location object that it returns will contain a time stamp. You can set a tolerance for the time difference between now and the Location's getTime()

Upvotes: 1

Related Questions