Reputation: 31
Does requestLocationUpdates()
retrieve cached location initially then subsequently gets fresh location?
We are working on an app that searches for local businesses using current location. The app has worked fine most of the time. But once it gave wrong location. Instead of current location, it returned a stale location. It was about 20 miles off and was about couple of hours old. In those two hours, I had not run any other app that may have used the GPS or network location. The phone was on all the time.
The app is running on Motorola Atrix 4G with ginger bread. Since we need location only once, we simply use the first location we get. The app intends to receive the location in foreground.
Please have a look at the code and see if there is anything wrong.
public class MyLocation {
LocationManager lm;
boolean network_enabled=false;
public static double lat = 0.0;
public static double lng = 0.0;
public boolean getLocation(Context context)
{
//I use LocationResult callback class to pass location value from MyLocation to user code.
if(lm==null)
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//exceptions will be thrown if provider is not permitted.
try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){ex.printStackTrace();}
//don't start listeners if provider is not enabled
if(!network_enabled)
return false;
if(network_enabled)
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
return true;
}
LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
lat = location.getLatitude();
lng = location.getLongitude();
lm.removeUpdates(this);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
}
Upvotes: 3
Views: 248
Reputation: 401
Receiving cached locations is commonly caused by calling getLastKnownLocation(LocationManager.NETWORK_PROVIDER). However, your code doesnt seem to use this method.
Working on an application similar to the one you described and using code similar to what you've written (code without getLastKnownLocation(LocationManager.NETWORK_PROVIDER)), We have observed times where requestLocationUpdates() returns a cached location, before retrieving 'fresh' locations.
I dont believe requestLocationUpdates() inherently returns a stale location. From androids documentation:
Getting user location in Android works by means of callback. You indicate that you'd like to receive location updates from the LocationManager ("Location Manager") by calling requestLocationUpdates(), passing it a LocationListener. Your LocationListener must implement several callback methods that the Location Manager calls when the user location changes or when the status of the service changes.
Receiving these stale locations is certainly uncommon from our testing, and seemed to affect older versions of android (4.0.4)
For our application, it was enough to ignore the first location returned from requestLocationUpdates() and simply use the second location.
Upvotes: 2