Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LocationManager returns old cached "Wifi" location with current timestamp

I am trying to get the current location. For that I implement a LocationListener and register it for both the network and the GPS provider:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

I block then for 30 seconds and use the first location that gets passed into the listener's

onLocationChanged()

method with an accuracy of 100 meters or better.

Most of the time this works fine. If the phone is connected to some Wifi network, it takes just a second to get a correct location with an accuracy of about 50 meters. If there is no Wifi but GPS is enabled, it can of course take a while to get a location.

Sometimes however, when connected to a Wifi and getting the current location, some old (cached?) previous "Wifi" location is provided - it might be 15 minutes old and 15 kilometers away from the current location. The problem is, that

location.getTime()

returns the current time - so it is impossible to know that the location is old.

I guess I have to implement a more elaborate solution - I would just like to know why these old "Wifi" locations have a current timestamp instead one from the time when it was originally retrieved.

like image 723
Torsten Römer Avatar asked Jul 16 '11 18:07

Torsten Römer


People also ask

Which of the following location provider is returned by locationManager?

Returns the current enabled/disabled state of location.

Which of the following method is called on the object of locationManager to get the current location?

First, create a Location Manager object by calling the getSystemService() method and LOCATION_SERVICE as an argument. Call getBestProvider() to get the location in the form of a string. Now the provider to getLastKnownLocation() as an argument to get the location.

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.

Which of the following methods is used to list all the location providers?

The best way is to use the “network” or “passive” provider first, and then fallback on “gps”, and depending on the task, switch between providers.


1 Answers

This is a known issue which I have encountered and did some research on why this happens.

Here are my observations:

  • Usually this happens when the mobile network hand-off is happening after losing network connectivity which may not necessarily be significant enough for the user to realize.
  • Consider you are taking a tube train and you get in at station A and get down at station B, now when you get down at station B the network cell ID may/maynot still be of station A and of course it will do a hands-off and move to station B.
  • However if you call for getLocation is active before the hand-off you would get station A location which might be like 10 km and 15 mins back.

First understand how network location works: Android has the cellId of the tower to which it is currently connected to and this id is then used by google to perform look-up and fetch approximate location information whose accuracy can range from 50 metres (one of the best) to a few thousand metres. If the cellId is incorrect as shown in the above example then you would receive wrong location.

There is not much you can do to avoid this except having a custom algorithm that can weed out this noise. Something like

if (location from network) {
    if (speed obtained from the difference between previous and current location is        greater than say 30 m/s) {
        ignore this location as noise
    } else {
       location is correct
    }
}
like image 169
PravinCG Avatar answered Oct 10 '22 21:10

PravinCG