Location Updates

One-Shot and Continuous Location Updates

getCurrentLocation() computes one fresh fix (lastLocation returns a cached, possibly stale one). To follow the user, register a LocationCallback; a callbackFlow makes it a Flow whose cancellation removes the callback when the screen leaves:

location/Branches.kt: one fused client, two ways to ask (excerpt)Kotlin
class BranchLocator(context: Context) {
  private val client = LocationServices.getFusedLocationProviderClient(context)
  @RequiresPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
  suspend fun current(): Location? =
    client.getCurrentLocation(Priority.PRIORITY_BALANCED_POWER_ACCURACY,
      CancellationTokenSource().token).await()
  @SuppressLint("MissingPermission")                        // checked by the caller
  fun updates(): Flow<Location> = callbackFlow {
    val request = LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 10_000)
      .setMinUpdateIntervalMillis(5_000)
      .build()
    val callback = object : LocationCallback() {
      override fun onLocationResult(result: LocationResult) {
        result.lastLocation?.let { trySend(it) }
      }
    }
    client.requestLocationUpdates(request, callback, Looper.getMainLooper())
    awaitClose { client.removeLocationUpdates(callback) }
  }
}

The interval is a wish: the provider batches and throttles, and setMinUpdateIntervalMillis caps the rate when other apps ask for more. With the branch screen open (--es stage branch), adb emu geo fix <longitude> <latitude> moved the emulator:

Moving the emulator from Sunnyvale to downtown San JoseShell
adb -s emulator-5556 emu geo fix -122.0363 37.3688
adb -s emulator-5556 emu geo fix -121.8950 37.3350
Output
D/BookNestLoc( 6805): one-shot: null
D/BookNestLoc( 6805): update 37.3688, -122.0363 -> BookNest Mountain View, 4.6 km
D/BookNestLoc( 6805): update 37.3350, -121.8950 -> BookNest San Jose, 0.4 km

The one-shot request returned null: on the freshly booted emulator no fix came before it gave up, while the continuous request waited. Phones return null too (location off, indoors), so treat it as "not yet".