Simple obtaining of the current or the last location with the Kotlin Coroutines
By default, there are 5-seconds timeout for operation
Also, it is checked whether the GPS adapter is enabled on the device and whether there are valid data to receive
If one of the conditions is not valid, then the corresponding exception is thrown:
- LocationDisabledException - if GPS-adapter is disabled
- SecurityException - if permissions (API >= 23) is not granted (
ACCESS_COARSE_LOCATION
forCGPS::lastLocation
, andACCESS_FINE_LOCATION
for otherwise) - TimeoutException - if location is not received in time (by default 5000ms)
- LocationException - others exceptions
- ServicesAvailabilityException - if Google or Huawei Services not available on device
- ResolutionNeedException (for GoogleCGPS/HuaweiCGPS only) - if user permission is requested to enable GPS using the system dialog
To query the current location, the accuracy of the determination (Accuracy.*) and the maximum timeout of the operation
For Huawei variant:
The library requires the
agconnect-services.json
file in the project, without it aLocationException
error will be thrown
In the lib are three version:
HardwareCGPS - not required Google or Huawei Services. Uses only Android Location Manager
More battery friendly variants:
- GoogleCGPS - uses Google Services
- HuaweiCGPS - for HMS devices
- Get last location
val location: Location = HardwareCGPS(context).lastLocation()
- Get actual location
val location: Location = HardwareCGPS(context).actualLocation()
- Get location updates
val flowLocation: Flow<Result<Location>> = HardwareCGPS(context).requestUpdates()
- Get actual location with enable GPS request (Google/Huawei implementation only)
val location: Location = GoogleCGPS(context).actualLocationWithEnable()
NOTE: Use ActivityResultApi with StartIntentSenderForResult
contract to handle ResolutionNeedException.intentSender
Artifact is publishing to Maven Central. You can add this repository to your project with:
repositories {
mavenCentral()
}
Add to your .gradle file:
- Core module (with hardware implementation)
implementation "io.github.geniusrus:cgps-core:$last_version"
- Google module (include Core)
implementation "io.github.geniusrus:cgps-google:$last_version"
- Huawei module (include Core)
implementation "io.github.geniusrus:cgps-huawei:$last_version"
Old artifact io.github.geniusrus:cgps
is still available at 2.1.0
version. But it will no longer be supported
The sample is on app
module
Apache v2.0 License
Copyright (c) 2018 Viktor Likhanov