LocationManager returns old cached “Wifi” location with current timestamp

前端 未结 3 591
再見小時候
再見小時候 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: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.

提交回复
热议问题