Get user location in Compose with M3

15 Views Asked by At

I am trying to get the User Location in Compose.

I found this link and tried the Mitch solution. But it has an infinite loop and the snackbar is not shown. From other links I read that it needs to be attached to a scaffold but it seems than on M3 is no longer valid.

I change the infinite loop from:

LaunchedEffect(true) {
    while (true) {
        isGPSEnabled = isGPSEnabled(context)
        delay(2.seconds)
    }
}

To:

LaunchedEffect(true) {
    while (!isGPSEnabled) {   //while (true) {
        isGPSEnabled = isGPSEnabled(context)
        delay(2.seconds)
    }
}

But I don't know how to make the snackbar to show:

DisposableEffect(
    isGPSEnabled,
    permissions.shouldShowRationale,
    permissions.allPermissionsGranted,
) {
    if (!permissions.allPermissionsGranted || permissions.shouldShowRationale) {
        scope.launch {
            val result = snackbarHostState.showSnackbar(
                "Missing required permissions",
                "Grant",
                withDismissAction = true,
                duration = SnackbarDuration.Indefinite,
            )

            when (result) {
                SnackbarResult.Dismissed -> {

                }

                SnackbarResult.ActionPerformed -> {
                    permissions.launchMultiplePermissionRequest()
                }
            }
        }
        return@DisposableEffect SimulatedDisposableEffectResult
    }

I know the code [val result = snackbarHostState.showSnackbar(...) ]is executed because I have a break point in debug that reach it. It stays there an never reach the [when(result)] since it has[duration = SnackbarDuration.Indefinite]

On my Screen I have the following to call the function:

Image(painter = painterResource(R.drawable.baseline_my_location_24),
                contentDescription = "Get Coordinates",
                modifier = Modifier
                    .clickable {
                        doGet = true
                    }
                    .size(40.dp)
            )

            if (doGet) {
                ExampleForegroundLocationTrackerScreen()
            }

That is for testing purpose, but I really want to receive the value(latitud, longitud) to show them on the screen and later save then in a Database.

0

There are 0 best solutions below