Daniel Florez
Daniel Florez

Reputation: 33

Android Kotlin - Strange behaviour getting location using fusedLocationProviderClient.getCurrentLocation

i'm currently developing an App in Android (Kotlin) to get the location (latitude and longitude) on demand (Only when pressing a float action button) and display them in some TextViews, i dont need constant updates of the location (requestLocationUpdates). In other function i'm checking permissions so all of that is apparently fine. I created the apk file, installed it on my phone (Motorola One Vision - Android 11) and i went to walk to test some locations and getting coordinates but i got something strange. When i pressed the Geolocation button i got the coordinates after a few seconds. After that if i were to walk like 5 or 10 meters (Like 8 to 15 seconds walking) and try to get the location again, 3 possible things happened:

I don't understand the behaviour of "getCurrentLocation" and i'm worried about the second case (Getting same coordinates than before). Why is that?, and what can i do to adress tha behaviour of getCurrentLocation?. I would like every time i press the button i get new coordinates even if i have to wait some seconds. And the third case is a weird one. How can i get a new coordinate so fast in some cases and in other (first case) i have to wait.

This is my code only of the function that activates the getCurrentLocation function.

    @SuppressLint("MissingPermission")
    fun getLocation(){
        if (isGPSActivated){
            fusedLocationProviderClient.getCurrentLocation(
                LocationRequest.PRIORITY_HIGH_ACCURACY,
                object : CancellationToken() {
                    override fun onCanceledRequested(p0: OnTokenCanceledListener) = CancellationTokenSource().token
                    override fun isCancellationRequested() = false
                })
                .addOnSuccessListener { location: Location? ->
                    if (location == null) {
                        Toast.makeText(
                            this,
                            "Can't get location, try again",
                            Toast.LENGTH_SHORT
                        ).show()
                    }

                    else {
                        updateLocationInViewModel(location)
                        
                        Toast.makeText(
                            this,
                            "Location obtained",
                            Toast.LENGTH_SHORT
                        ).show()
                    }
                }
                .addOnFailureListener {
                    Toast.makeText(
                        this,
                        "Can't get location, try again",
                        Toast.LENGTH_SHORT
                    ).show()
                }
        }
    }



    fun updateLocationInViewModel(currentLocation: Location){
        individuoViewModel._localization.value = currentLocation
    }

I have an observer for individuoViewModel._localization so that when it detects a new value the TextViews refresh.

Upvotes: 0

Views: 602

Answers (1)

Daniel Florez
Daniel Florez

Reputation: 33

The getCurrentLocation() function is using the Fused Location Provider API in Android to get the device's current location. The priority is set to high accuracy, which means that the device will use GPS, Wi-Fi, and mobile networks to determine the location. The behavior you're observing is likely due to the way the Fused Location Provider API works.

When you first request the location, the device will use GPS to determine the location, which can take a few seconds. If you request the location again within a few minutes of the first request, the device will likely use the cached location instead of requesting a new location from the GPS, which is why you're seeing the location obtained almost instantly. This can happen if the device is unable to determine a new location using GPS.

When you move a few meters and request the location again, the device may use the GPS to determine the new location, which can take a few seconds. However, if the device is still unable to determine a new location using GPS, it may return the cached location again.

It's normal that you don't see the little icon of google location in the status bar, this icon appears when the device is actively using GPS to determine the location, but it doesn't appear when the device is using cached location or other sources like wifi or mobile network.

In conclusion, the Fused Location Provider API uses a combination of GPS, Wi-Fi, and mobile networks to determine the device's location, and it may return a cached location if the device is unable to determine a new location using GPS. The time it takes to get a location and the accuracy of the location can vary depending on the device's environment and the availability of location sources.

Upvotes: 1

Related Questions