mapbox / mapbox-maps-android

Interactive, thoroughly customizable maps in native Android powered by vector tiles and OpenGL.
https://www.mapbox.com/mobile-maps-sdk
Other
464 stars 131 forks source link

rememberMapViewportState not working on 11.5.0 #2430

Open D35P4C1T0 opened 2 months ago

D35P4C1T0 commented 2 months ago

Environment

Observed behavior and steps to reproduce

In version 11.3.1, I was able to create the MapboxMap composable with

MapboxMap(
        modifier.fillMaxSize(),
        mapViewportState = mapViewportState,
        locationComponentSettings = LocationComponentSettings
            .Builder(createDefault2DPuck(withBearing = true))
            .setEnabled(true)
            .setPuckBearingEnabled(true)
            .setPuckBearing(PuckBearing.HEADING)
            .build(),
        style = { MapStyle(style = Style.MAPBOX_STREETS) },
        scaleBar = { }, // no scale bar
    )

But with the lastest version (11.5.0), the localComponentSettings had been moved. Omitting it will turn my code into

MapboxMap(
        modifier.fillMaxSize(),
        mapViewportState = mapViewportState,
        style = { MapStyle(style = Style.MAPBOX_STREETS) },
        scaleBar = { }, // no scale bar
    )

But these changes also seem to have changed the behavior of rememberMapViewportState. The state is no longer maintained, for example, by switching between screens with a NavController. Am I missing somethings?

Expected behavior

The viewportState is preserved across recompositions.

Notes / preliminary analysis

The code above works for version 11.3.1. I might be adapting my old code in a way that breaks rememberMapViewportState. With version 11.5.0, navigation causes recomposition of my Nav composable. With version 11.3.1, the Nav component my Map is placed in is not recomposed.

Additional links and references

pengdev commented 2 months ago

Hey @D35P4C1T0 could you share some code snippets to reproduce your issue? e.g. where do you rememberMapViewportState and how the recomposition is done with the Nav composable?

D35P4C1T0 commented 1 month ago

I'll start by saying that I'm building my first android app for a uni project. This won't likely be a well structured project.

Here's my project structure:

MainActivity {
  MyApp { @Composable
    val navController = rememberNavController()
    val mapViewportState = rememberMapViewportState {}
    Scaffold { (from material3)
      CenterAlignedTopAppBar, BottomNavigationBar, 
      content = NavHost( navController = navController ) { \* all the routes here *\ }
    }
  }
}

And the NavHost composable has pretty much all my app routes, which are in the shape of

composable("routeName"){ PageContent() } 

Now, the "map" route can be depicted as follows:

composable("map") { MapScreen( mapViewportState = mapViewportState, otherParams ) }

And my MapScreen composable is basically a container for the MapboxMap composable:

MapboxMap(
        modifier.fillMaxSize(),
        mapViewportState = mapViewportState,
        style = { MapStyle(style = Style.MAPBOX_STREETS) },
        scaleBar = { }, // no scale bar
)

I hope it helps. I don't think I'm triggering unexpected recompositions of the MapboxMap or MapScreen composables.

pengdev commented 1 month ago

@D35P4C1T0 thanks for the code snippets, from these snippets I don't see anything suspicious, would it be possible to share the minimum working project to reproduce the issue?

navigation causes recomposition of my Nav composable.

Which composable is recomposed? the NavHost?