Skip to content

Commit d87c1f3

Browse files
committed
fix: updating DisappearingScaleBar
1 parent 9293ad0 commit d87c1f3

File tree

1 file changed

+16
-29
lines changed
  • maps-compose-widgets/src/main/java/com/google/maps/android/compose/widgets

1 file changed

+16
-29
lines changed

maps-compose-widgets/src/main/java/com/google/maps/android/compose/widgets/ScaleBar.kt

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -72,29 +72,25 @@ public fun ScaleBar(
7272
lineColor: Color = DarkGray,
7373
shadowColor: Color = Color.White,
7474
) {
75+
var horizontalLineWidthMeters by remember {
76+
mutableIntStateOf(0)
77+
}
78+
79+
LaunchedEffect(key1 = cameraPositionState.position) {
80+
val upperLeftLatLng = cameraPositionState.projection?.fromScreenLocation(Point(0, 0)) ?: LatLng(0.0, 0.0)
81+
val upperRightLatLng = cameraPositionState.projection?.fromScreenLocation(Point(0, width.value.toInt())) ?: LatLng(0.0, 0.0)
82+
val canvasWidthMeters = upperLeftLatLng.sphericalDistance(upperRightLatLng)
83+
val eightNinthsCanvasMeters = (canvasWidthMeters * 8 / 9).toInt()
84+
85+
horizontalLineWidthMeters = eightNinthsCanvasMeters
86+
}
87+
7588
Box(
76-
modifier = modifier
77-
.size(width = width, height = height)
89+
modifier = modifier.size(width = width, height = height)
7890
) {
79-
var horizontalLineWidthMeters by remember {
80-
mutableIntStateOf(0)
81-
}
82-
8391
Canvas(
8492
modifier = Modifier.fillMaxSize(),
8593
onDraw = {
86-
// Get width of canvas in meters
87-
val upperLeftLatLng =
88-
cameraPositionState.projection?.fromScreenLocation(Point(0, 0))
89-
?: LatLng(0.0, 0.0)
90-
val upperRightLatLng =
91-
cameraPositionState.projection?.fromScreenLocation(Point(0, size.width.toInt()))
92-
?: LatLng(0.0, 0.0)
93-
val canvasWidthMeters = upperLeftLatLng.sphericalDistance(upperRightLatLng)
94-
val eightNinthsCanvasMeters = (canvasWidthMeters * 8 / 9).toInt()
95-
96-
horizontalLineWidthMeters = eightNinthsCanvasMeters
97-
9894
val oneNinthWidth = size.width / 9
9995
val midHeight = size.height / 2
10096
val oneThirdHeight = size.height / 3
@@ -189,13 +185,6 @@ public fun ScaleBar(
189185
}
190186
}
191187

192-
/**
193-
* An animated scale bar that appears when the zoom level of the map changes, and then disappears
194-
* after [visibilityDurationMillis]. This composable wraps [ScaleBar] with visibility animations.
195-
*
196-
* Implement your own observer on camera move events using [CameraPositionState] and pass it in
197-
* as [cameraPositionState].
198-
*/
199188
@Composable
200189
public fun DisappearingScaleBar(
201190
modifier: Modifier = Modifier,
@@ -212,12 +201,10 @@ public fun DisappearingScaleBar(
212201
val visible = remember {
213202
MutableTransitionState(true)
214203
}
215-
216-
LaunchedEffect(key1 = cameraPositionState.position.zoom) {
217-
// Show ScaleBar
204+
// This effect controls visibility, not data updates
205+
LaunchedEffect(key1 = cameraPositionState.position) {
218206
visible.targetState = true
219207
delay(visibilityDurationMillis.toLong())
220-
// Hide ScaleBar after timeout period
221208
visible.targetState = false
222209
}
223210

0 commit comments

Comments
 (0)