Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How reliable is LocationManager's getLastKnownLocation and how often is it updated?

I am building an app that can use a user's current location on certain actions. Location is more of a benefit to the user rather than a critical part of the process. I'm only interested in very rough accuracy and it can be off by 5 or even 10 miles and still be of value. General plan was to see if the network provider was enabled and then just do

locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)

Assuming that no other apps are running, will the phone automatically periodically update the network location? I.e. there will likely always be a value returned by this code which will meet my needs?

EDIT:

I've been doing a bit more research and I think the question I was really trying to ask here was if the Android operating system or the phone itself would update the last known location for the network provider as the phone locked onto different cell phone towers or wifi networks. The answer appears to be no. After force stopping all apps on my phone which I know to interaction with location services, the last known location has stopped updating. So, I'm guessing that these days chances are that most phones will have some location services running in the background updating the last known location but the phone itself won't do it. Hence I think I'll be going with some form of requesting location updates if the last known location is too old.

like image 669
Chris Knight Avatar asked Sep 14 '11 22:09

Chris Knight


People also ask

What is the use of LocationManager in android?

LocationManager is the main class through which your application can access location services on Android. Similar to other system services, a reference can be obtained from calling the getSystemService() method.

Which method is used to list all the location providers in android?

LocationProvider is the super class for location providers. A location provider provides the information of the geographical location of the device. This information is stored in the Location class.

How do I turn off location listener on android?

You can stop the LocationListener by making its object to null after stoping LocationListener locationManager. removeUpdates(mLocListener); , that is mLocListener = null; when you want it to stop fetching the Latitudes and Longitudes. You can stop continuous location updating by calling method locationManager.


2 Answers

getLastKnownLocation() only returns the last fix. So if no location providers are being updated the return value of getLastKnownLocation() will not change. The location object will also provide you with accuracy and time of the fix.

I would look at this post for more information. You could use some version of the one shot location.

http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html

like image 121
Frohnzie Avatar answered Sep 27 '22 00:09

Frohnzie


Frohnzie is correct about getLastKnownLocation. Getting location is an expensive operation, so it only happens when an app requests it. I've often driven 30 miles or so, opened the foursquare app on my phone, and it showed me venus that were 30 miles away. Clearly they were using getLastKnownLocation.

For your use case, I would recommend LocationManager.requestLocationUpdates. You can specify an accuracy and a timeout. So if the phone can't quickly find a rough estimate of the user's location, you can just forego location since it is not crucial to your application.

like image 22
michaelg Avatar answered Sep 27 '22 00:09

michaelg