Skip to content

Commit

Permalink
feat!: setting the custom location button via sample (#428)
Browse files Browse the repository at this point in the history
* feat: adding custom location button to sample

* doc: added explanatory comment

BREAKING CHANGE: remove `myLocationButton` from `GoogleMap`. Equivalent functionality for customizing a map control is demonstrated in the sample app.
  • Loading branch information
kikoso committed Oct 25, 2023
1 parent f7a2e18 commit cebb6a0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
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

0 comments on commit cebb6a0

Please sign in to comment.