How to implement fusedprovider on request location updates

25 views Asked by At

How can I implement on request location updates so that instead of getting the users last location I can constantly get their current location. Code is: https://pastebin.com/8vLb0NF6 this is what I have right now for getting the user location:

    private fun getCurrentLocation() {
    if(checkPermissions()){
        if(isLocationEnabled()){
            if (ActivityCompat.checkSelfPermission(
                    this,
                    Manifest.permission.ACCESS_FINE_LOCATION
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                requestPermissions()
                return
            }
            fusedLocationProviderClient.lastLocation.addOnCompleteListener(this){
                task -> val location: Location?=task.result
                if(location == null){
                    Toast.makeText(this, "Unknown exception", Toast.LENGTH_SHORT).show()
                }
                else{
                    Toast.makeText(this, "Great Success", Toast.LENGTH_SHORT).show()





                }
            }
        }
        else{
            Toast.makeText(this, "Turn on your location", Toast.LENGTH_SHORT).show()
            val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
            startActivity(intent)
        }
    }
    else{
        requestPermissions()
    }


}
1

There are 1 answers

0
GlintShadow On

The short demo program in my question here

Android location is providing timestamps in the future, or am I stupid? Try it for yourself

provides a reliable method to get regular location updates. You can change GPS_PROVIDER to FUSED_PROVIDER in the demo in one place.

I don't know what your application is but be aware that location fixes can suffer from 'jitter' (jumping around by a few meters) and sometimes step-function changes (when the "best three" satelites change for example). Different phones behave quite differently to each other when I compare them side by side on the same walk!

Please also note that often the first few locations jump around more than later.

I hope this helps.