πŸ”Geolocator

The compass-geolocation artifact provides an interface Geolocator that has members for:

  • Getting the current location

  • Starting and stopping tracking the location

  • Check if location services are available

Creating a Geolocator

To get an instance of Geolocator you can use the factory function:

public fun Geolocator(
    locator: Locator,
    dispatcher: CoroutineDispatcher = Dispatchers.Default,
): Geolocator

Locator

A Locator is interface that can be implemented to provide all of the geolocation functionality.

Compass provides Locator's for Android / iOS and the Browser:

// Android and iOS
val locator = Locator.mobile()

// Or for the browser
val locator = Locator.browser()

// Create the geolocator
val geolocator: Geolocator = Geolocator(locator)

There are also included extension functions for each artifact:

There are similar extension functions for the Browserartifact.

Make sure you add the proper dependencies, see All Dependencies.

Custom Locator

If you would like to provide your own you can implement the object yourself:

Permissions

Compass will handle the permissions for Android and iOS automatically for you, see Android / iOS.

Current location

To get the current location of the device call Geolocator.current(). It will return a GeolocatorResult, or you can use one of the extension functions:

GeolocatorResult

When you call Geolocator.current() you will receive a GeolocationResult. It can be one of the following:

  • GeolocatorResult.Success

    • This has contains a Location object of the current location details.

  • GeolocatorResult.Error

    • NotSupported: Geolocation is not supported on this device

    • GeolocationFailed: Geolocation failed for an unknown reason

    • PermissionError: We don't have permission to use the geolocation services

Tracking location

You can subscribe to location updates by collecting the Geolocator.locationUpdates, or to get updates to the status of tracking (errors, permissions, updates) use Geolocator.trackingStatus:

Location details

You can read about what kind of data is in the Location object by reading Location.

Last updated

Was this helpful?