Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: setting the custom location button via sample #428

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp


class CustomControlsActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContent {
var isMapLoaded by remember { mutableStateOf(false) }
val mapProperties by remember { mutableStateOf(MapProperties(isMyLocationEnabled = true)) }
// This needs to be manually deactivated to avoid having a custom and the native
// location button
val uiSettings by remember { mutableStateOf(MapUiSettings(myLocationButtonEnabled = false)) }
// Observing and controlling the camera's state can be done with a CameraPositionState
val cameraPositionState = rememberCameraPositionState {
position = defaultCameraPosition
Expand All @@ -59,17 +62,16 @@ class CustomControlsActivity : ComponentActivity() {
onMapLoaded = {
isMapLoaded = true
},

myLocationButton = {
MapButton(
"This is a custom location button",
onClick = {
Toast.makeText(
this@CustomControlsActivity,
"Click on my location",
Toast.LENGTH_SHORT
).show()
})
uiSettings = uiSettings,
)
MapButton(
"This is a custom location button",
onClick = {
Toast.makeText(
this@CustomControlsActivity,
"Click on my location",
Toast.LENGTH_SHORT
).show()
})

if (!isMapLoaded) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import android.location.Location
import android.os.Bundle
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Composition
import androidx.compose.runtime.CompositionContext
Expand Down Expand Up @@ -88,7 +87,6 @@ public fun GoogleMap(
onMyLocationClick: ((Location) -> Unit)? = null,
onPOIClick: ((PointOfInterest) -> Unit)? = null,
contentPadding: PaddingValues = NoPadding,
myLocationButton: (@Composable @GoogleMapComposable () -> Unit)? = null,
content: (@Composable @GoogleMapComposable () -> Unit)? = null,
) {
// When in preview, early return a Box with the received modifier preserving layout
Expand Down Expand Up @@ -119,16 +117,11 @@ public fun GoogleMap(
val currentContentPadding by rememberUpdatedState(contentPadding)

// If we pass a custom location button, the native one is deactivated.
val currentUiSettings by rememberUpdatedState(if (myLocationButton != null) {
uiSettings.copy(myLocationButtonEnabled = false)
} else {
uiSettings
})
val currentUiSettings by rememberUpdatedState(uiSettings)
val currentMapProperties by rememberUpdatedState(properties)

val parentComposition = rememberCompositionContext()
val currentContent by rememberUpdatedState(content)
val currentLocation by rememberUpdatedState(myLocationButton)

LaunchedEffect(Unit) {
disposingComposition {
Expand All @@ -150,10 +143,6 @@ public fun GoogleMap(
}
}
}
Row(modifier = modifier) {
currentLocation?.invoke()
}

}

internal suspend inline fun disposingComposition(factory: () -> Composition) {
Expand Down
Loading