LocationManager returns old cached “Wifi” location with current timestamp

前端 未结 3 579
再見小時候
再見小時候 2020-12-25 09:14

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.r         


        
相关标签:
3条回答
  • 2020-12-25 09:17

    This is helpful:

    A Deep Dive Into Location

    and lastly the source code for that talk:

    android-protips-location

    0 讨论(0)
  • 2020-12-25 09:26

    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
        }
    }
    
    0 讨论(0)
  • 2020-12-25 09:37

    I have been facing the same issues until I made some changes to my code.

    What happened is that I was attaching the same LocationListener when requesting for both GPS and Network location updates and I was getting "weird" issues including getting old WIFI location updates with current time.

    Here's my old code:

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

    Apparently that is a rather "unsafe" thing to do (sorry, Android newbie here) and so I changed it to:

    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0, networkLocationListener);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, gpsLocationListener);
    

    Of course I had to define 2 separate onLocationChanged block of codes to handle the 2 listeners.

    Well, it did solve my problem. I tested this on Gingerbread (API Level: 8). Not sure if it works for you.

    0 讨论(0)
提交回复
热议问题