LocationHelper

class LocationHelper(context: Context)(source)

Centralized location helper based on Google Play Services FusedLocationProviderClient.

This utility provides high-level APIs for fetching the last known location, getting a single fresh fix, or observing a continuous stream of updates.

Requirements:

  • Google Play Services dependency in android/build.gradle.kts

  • Permissions: ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION

Example usage:

val locationHelper = LocationHelper(context)

// Get last known location (fast, can be null)
val last = locationHelper.getLastLocation()

// Get single fresh fix
val location = locationHelper.getCurrentLocation(LocationConfig.HighAccuracy)

// Continuous stream
viewModelScope.launch {
locationHelper.locationFlow(LocationConfig.Balanced).collect { location ->
updateMap(location)
}
}

Constructors

Link copied to clipboard
constructor(context: Context)

Functions

Link copied to clipboard
@RequiresPermission(anyOf = ["android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION"])
suspend fun getCurrentLocation(config: LocationConfig = LocationConfig.Balanced): Location?
Link copied to clipboard
@RequiresPermission(anyOf = ["android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION"])
suspend fun getLastLocation(): Location?
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
@RequiresPermission(anyOf = ["android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION"])
fun locationFlow(config: LocationConfig = LocationConfig.Balanced): Flow<Location>