Animated User LocationPuck #656
-
|
Hi, I’m trying to implement the user location indicator with a small animation, but I can’t get the transition to look smooth. For some reason, the interpolated positions cause a jitter on the vertical axis. Any idea how this could be done? I’ve seen the example in the Native documentation, but I can’t get it to work here in Compose 🙏. This is the interpolated location between 2 fixed coordinates // latLng is the latest user position detected, updated every few seconds
@MaplibreComposable
@Composable
fun LocationPuck(latLng: LatLng) {
val positionAnimator = remember {
Animatable(Position(latLng.longitude, latLng.latitude), PositionConverter)
}
LaunchedEffect(latLng) {
launch {
positionAnimator.animateTo(
targetValue = Position(longitude = latLng.longitude, latitude = latLng.latitude),
animationSpec = tween(
durationMillis = LocationProvider.CONTINUOUS_LOCATION_UPDATE_INTERVAL_MS.toInt(),
easing = LinearEasing
)
)
}
}
val featurePoint by remember(positionAnimator.value) {
derivedStateOf {
Feature(
geometry = Point(positionAnimator.value),
)
}
}
val source = rememberGeoJsonSource(
data = GeoJsonData.Features(featurePoint)
)
LocationPuckLayers(
locationSource = source,
accuracy = 5.5,
metersPerDp = 0.0
)
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
How does your PositionConverter work? I assume it's converting the lat/long into single-precision (32 bit) floats, which are inherently imprecise at the scale of earth lat/lon. Last I checked, Compose doesn't have animation helpers that operate on double-precision (64 bit) floats. You could mitigate the floating point imprecision somewhat by offsetting the lat/lon so the numbers are closer to zero (essentially, lat/lon relative to some point near where you're animating). This is known as a "floating origin". Or perhaps animate some tweening value between 0-1, and use that value to transform the point along its path (origin at 0, destination at 1). |
Beta Was this translation helpful? Give feedback.


How does your PositionConverter work? I assume it's converting the lat/long into single-precision (32 bit) floats, which are inherently imprecise at the scale of earth lat/lon. Last I checked, Compose doesn't have animation helpers that operate on double-precision (64 bit) floats.
You could mitigate the floating point imprecision somewhat by offsetting the lat/lon so the numbers are closer to zero (essentially, lat/lon relative to some point near where you're animating). This is known as a "floating origin".
Or perhaps animate some tweening value between 0-1, and use that value to transform the point along its path (origin at 0, destination at 1).