The ion-android-geolocation is a library built using Kotlin that provides geolocation features for Android applications.
The IONGLOCController class provides the main features of the Library, which are:
- obtaining the location/position of the device a single time;
- adding a watch to obtain periodic location updates;
- clearing/removing a previously added watch, turning off location updates.
This library is used by the Geolocation Plugin for OutSystems' Cordova Plugin and Ionic's Capacitor Plugin.
Replace ${version to use} below with the version you want to use. Check the Releases page for available versions.
In your app-level gradle file, import the ion-android-geolocation library like so:
dependencies {
implementation("io.ionic.libs:iongeolocation-android:${version to use}")
}
IONGLOCLocationButtonRegistry.register(controller, ...) registers the
os.locationButton native component. The host plugin exposes the corresponding web
component. The button is the Jetpack Compose LocationButton from
androidx.core.locationbutton:locationbutton-compose, hosted in a ComposeView. The permission
flow is handled by that LocationButton itself: the system button on Android 17+, and a locally
rendered button with its default permission flow on older versions (see the Activity requirement
below).
The component emits grant, position, and buttonError events.
When the host plugin is destroyed, it calls IONGLOCLocationButtonRegistry.unregister(controller),
so the registration no longer holds onto it. A controller registered since stays registered.
Since version 4.0.0, the library ships as a single AAR that includes the Location Button, so it requires:
compileSdk37 or higher (apps built with MABS 13).aarMetadataenforces this.- Android Gradle Plugin 9.1 or higher and Kotlin 2.1 or higher in the consuming build, because
androidx.core.locationbuttonrequires them. minSdk26 or higher.- The host Activity must be a
ComponentActivity(AppCompatActivityis one), because the Compose button finds its Activity and the permission launcher (anActivityResultRegistryOwner) through it.
androidx.core.locationbutton:locationbutton-compose is still an alpha release
(1.0.0-alpha01, the latest published at the time of writing) and its API may change
incompatibly. The version is pinned, not a range. Expect
roughly 1 MB more in an APK built with R8, and several MB more without it. The core.locationbutton library should
be reviewed regularly and updated when newer (more stable) ones are published.
As mentioned before, the library offers the IONGLOCController class that provides the following methods to interact with:
suspend fun getCurrentPosition(
activity: Activity,
options: IONGLOCLocationOptions
): Result<IONGLOCLocationResult>The method is composed of the following input parameters:
- activity: the
Activityfrom the app using the library to use when obtaining the location. - options:
IONGLOCLocationOptionswith the options with which to make the location request with (e.g. timeout).
The method returns a Result containing either an object of type IONGLOCLocationResult, which includes the geolocation data (e.g. latitide, longitude), or an exception that should be handled by the caller app.
fun addWatch(
activity: Activity,
options: IONGLOCLocationOptions,
watchId: String
): Flow<Result<List<IONGLOCLocationResult>>>The method is composed of the following input parameters:
- activity: the
Activityfrom the app using the library to use when obtaining the location updates. - options:
IONGLOCLocationOptionswith the options with which to make the location updates request with (e.g. timeout). - watchId: a unique id identifying the watch to add, so that it can be removed later.
The method returns a Flow in which the location updates will be emitted to.
fun clearWatch(id: String): BooleanThe method is composed of the following input parameters:
- id: the
watchIdidentigying the watch to remove.
The method returns a Boolean indicating if the watch was cleared or not (in case the watch isn't found).
Common issues and solutions:
-
Location updates not received
- Check that location permission is allowed on the device
- Verify location services are enabled on the device
- Ensure the necessary permissions are included in
AndroidManifest.xml
-
Poor accuracy
- Enable high accuracy mode
- Ensure clear sky view
- Wait for better GPS signal
-
Error received when in airplane mode
- Try setting
IONGLOCLocationOptions.enableLocationManagerFallbackto true - available since version 2.0.0 - Keep in mind that only GPS signal can be used if there's no network, in which case it may only be triggered if the actual GPS coordinates are changing (e.g. walking or driving).
- Try setting
-
Timeout received in
watchPosition- Use the
IONGLOCLocationOptions.intervalparameter, introduced in version 2.1.0, and set it to belowtimeout, in order to try to receive a first location update before timing out. - Increase the
IONGLOCLocationOptions.timeoutvalue, if your use case can wait for some time. - Increase
IONGLOCLocationOptions.maximumAgeto allow to retrieve an older location quickly for the first update.
- Use the
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
ion-android-geolocation is available under the MIT license. See the LICENSE file for more info.
- Report issues on our Issue Tracker