as Gabe Sechan suggested you have to call for new location if you don't want your location to be null or require a fresh location everytime.
you can do it using getCurrentLocation() method which takes priority and a cancellation token.
val priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY
val cancellationTokenSource = CancellationTokenSource()
fusedLocationClient.getCurrentLocation(priority, cancellationTokenSource.token)
.addOnSuccessListener { location ->
Log.d("Location", "location is found: $location")
}
.addOnFailureListener { exception ->
Log.d("Location", "Oops location failed with exception: $exception")
}
you can change the priority based on your requirement as of now i have used PRIORITY_BALANCED_POWER_ACCURACY as it will search location using wifi and GPS to find location if you required with higher accuracy you can use PRIORITY_HIGH_ACCURACY
and we provide cancellation token as if in future we're not required location for e.g. Activity has been closed by user so we will cancel request using cancellationTokenSoure.cancel()
Videos
How does the fusedLocationProviderClient work in fine/coarse permission modes ?
-
What is the minimum least required network to fetch coarse data ?
-
Is it possible with only Wifi ? (For a device with No SIM cards/eSiM)
-
Or a SIM card is a must to get the coarse mode to fetch approx. location ?
In all of the above cases, the location is turned on, I think its the permission that determines whether to use the GPS or not. So with only coarse persimmon given, what are the min requirements ?
Edit 1: I have tried with WiFi On/Off, location accuracy on/off, wifi scanning on/off, bluetooth scanning on/off, with/without connected to wifi. None of these are able to give me an approximate/Coarse location (returns null).
Edit 2: I don't want to get the FINE/Precise location. And yes, this mode works.
as Gabe Sechan suggested you have to call for new location if you don't want your location to be null or require a fresh location everytime.
you can do it using getCurrentLocation() method which takes priority and a cancellation token.
val priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY
val cancellationTokenSource = CancellationTokenSource()
fusedLocationClient.getCurrentLocation(priority, cancellationTokenSource.token)
.addOnSuccessListener { location ->
Log.d("Location", "location is found: $location")
}
.addOnFailureListener { exception ->
Log.d("Location", "Oops location failed with exception: $exception")
}
you can change the priority based on your requirement as of now i have used PRIORITY_BALANCED_POWER_ACCURACY as it will search location using wifi and GPS to find location if you required with higher accuracy you can use PRIORITY_HIGH_ACCURACY
and we provide cancellation token as if in future we're not required location for e.g. Activity has been closed by user so we will cancel request using cancellationTokenSoure.cancel()
LastKnownLocation returning null is the norm. If the location subsystem wasn't already on, it doesn't know what the location was, so it returns null. If you want to ensure that you don't get null, request updates.
LastKnonLocation should only be used as a mild optimization if you really know what you're doing. Otherwise, don't call it.
We are currently working on an App that does a lot with users' locations. Therefore we are evaluating FusedLocationProviderClient and the "old" LocationManager to access the user's location over a long period of time.
The accuracy of the location is extremely important and we want to get the maximum out of the device. Initial testing showed great results with the "new" fused location provider. However, on some devices, this client is not available and in very rare cases the locations get really inaccurate after some time (<10min).
For some devices/cases we are thinking of using the "old" LocationManager, but my question is if this LocationManager will stick around in the future. I did some research but I wasn't able to find a lot of details. Google just "highly recommends switching to FusedLocationProviderClient", but it's missing some features of the LocationManager such as providing GNSS information.
Is it a good idea to use LocationManager and all the information that it provides such as GNSS Info?
Maybe some of you also did some research on this topic and can share the results.