[go: nahoru, domu]

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

Polyline doesn't recompose when points change #105

Open
PeterAttardo opened this issue Apr 29, 2022 · 9 comments
Open

Polyline doesn't recompose when points change #105

PeterAttardo opened this issue Apr 29, 2022 · 9 comments
Assignees
Labels
priority: p3 Desirable enhancement or fix. May not be included in next release. status: blocked Resolving the issue is dependent on other work. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@PeterAttardo
Copy link

Environment details

Android API 26
com.google.maps.android:maps-compose:2.1.0

Steps to reproduce

  1. Create a map with a MutableStateList of points
  2. Use the points to draw markers and a polyline between them
  3. Add functionality to add or remove points from the list
  4. Observe that the markers update, but the polyline does not

Code example

@Composable
fun MapScreen(){
    val waypoints = remember{ mutableStateListOf<LatLng>()}
    Map(waypoints) {
        waypoints.add(it)
    }
}

@Composable
private fun Map(
    waypoints: List<LatLng>,
    modifier: Modifier = Modifier,
    onMapClicked: (latlng: LatLng) -> Unit
){
    GoogleMap(
        modifier = modifier,
        
    ){
        waypoints.forEach {
            Marker(state = MarkerState(position = it))
        }
        Polyline(waypoints)
    }
}
@PeterAttardo PeterAttardo added triage me I really want to be triaged. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Apr 29, 2022
@jpoehnelt
Copy link
Contributor

@PeterAttardo Please take a moment to fill out this short survey. Thank you!

This is an automated message, feel free to ignore.

@jpoehnelt
Copy link
Contributor

@PeterAttardo Thank you for opening this issue. 🙏
Please check out these other resources that might be applicable:

This is an automated message, feel free to ignore.

@arriolac
Copy link
Member

This should be fixed. But a workaround would be to use a mutableStateOf instead. So:

@Composable
fun MapScreen(){
    val waypoints by remember{ mutableStateOf<List<LatLng>>()}
    Map(waypoints) {
        waypoints = waypoints + it
    }
}

@barbeau
Copy link
Contributor
barbeau commented May 11, 2022

@arriolac Was this a bug in maps-compose or Compose itself? And specifically handling mutableStateListOf()?

I think the other potential catch here is to make sure you're using mutableStateOf<List<LatLng>>() and not mutableStateOf<ArrayList<LatLng>>(), similar to my comment here regarding recomposition and mutable collections - #91 (comment).

@arriolac
Copy link
Member

I believe the issue might be between the interaction of mutableStateListOf and a ComposeNode's Updater. Because Polyline as currently implemented should work. I've opened an issue in the public issue tracker for Compose here: https://issuetracker.google.com/issues/232271525

@barbeau barbeau added status: blocked Resolving the issue is dependent on other work. priority: p3 Desirable enhancement or fix. May not be included in next release. and removed triage me I really want to be triaged. labels May 11, 2022
@Sergiyss
Copy link

Is the question still open? Help me. the last Polyline is not updated, but markers are drawn according to the route.

@Composable
fun MapScreen(){
  val path  = chvm.pathResult.observeAsState()
    var waypoints by remember{ mutableStateOf<List<LatLng>>(value = listOf())}

    if(path.value?.path?.size != null){
        Map(path.value?.path!!) {
            waypoints = waypoints + it
        }
    }
}

@Composable
private fun Map(
    waypoints: List<LatLng>,
    modifier: Modifier = Modifier,
    onMapClicked: (latlng: LatLng) -> Unit
){
    GoogleMap(
        modifier = modifier,
        
    ){
        waypoints.forEach {
            Marker(state = MarkerState(position = it))
        }
        Polyline(waypoints)
    }
}

Screenshot_202

@weslleystos
Copy link
weslleystos commented Mar 15, 2023

I found the same issue:

But when I was investigating the problem, I notice that when show the list in logcat.

D/MainActivity:androidx.compose.runtime.snapshots.SnapshotStateList@f1e2953

In this case we don't send the list in the end.

TrackerLocationTheme {
    val cameraPositionState = rememberCameraPositionState()
    val waypoints = remember { mutableStateListOf<LatLng>() }

    LocationService.lastLocation?.let {
        waypoints.add(it)
    }

    Scaffold(
        topBar = {
            TopAppBar(title = {
                Text(text = getString(R.string.app_name))
            })
        },
    ) { paddingValues ->
        GoogleMap(
            modifier = Modifier
                .fillMaxSize()
                .padding(paddingValues),
            cameraPositionState = cameraPositionState,
            properties = MapProperties(
                isMyLocationEnabled = true,
                mapStyleOptions = MapStyleOptions.loadRawResourceStyle(
                    LocalContext.current,
                    R.raw.map_style
                )
            ),
        ) {
            Polyline(points = waypoints.toList())
        }
    }
}

if you transform that toList, it will works fine.

@Ruineie
Copy link
Ruineie commented Apr 5, 2023

I found the same issue:

But when I was investigating the problem, I notice that when show the list in logcat.

D/MainActivity:androidx.compose.runtime.snapshots.SnapshotStateList@f1e2953

In this case we don't send the list in the end.

TrackerLocationTheme {
    val cameraPositionState = rememberCameraPositionState()
    val waypoints = remember { mutableStateListOf<LatLng>() }

    LocationService.lastLocation?.let {
        waypoints.add(it)
    }

    Scaffold(
        topBar = {
            TopAppBar(title = {
                Text(text = getString(R.string.app_name))
            })
        },
    ) { paddingValues ->
        GoogleMap(
            modifier = Modifier
                .fillMaxSize()
                .padding(paddingValues),
            cameraPositionState = cameraPositionState,
            properties = MapProperties(
                isMyLocationEnabled = true,
                mapStyleOptions = MapStyleOptions.loadRawResourceStyle(
                    LocalContext.current,
                    R.raw.map_style
                )
            ),
        ) {
            Polyline(points = waypoints.toList())
        }
    }
}

if you transform that toList, it will works fine.

This works! Thank you.

@marcportabellanavarro
Copy link
marcportabellanavarro commented Apr 4, 2024

This still happens at least in 4.2.0. I need to test in the latest. Lucky I found this issue otherwise I would've lost a good time going mad over this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p3 Desirable enhancement or fix. May not be included in next release. status: blocked Resolving the issue is dependent on other work. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

8 participants