0

I have a google map used in my compose project. I meet some conflicts between keeping the CameraPositionState across navigation and avoiding crash.I only have two screens to navigate between btw. At first, I put the rememberCameraPositionState in the MapScreen as the docs suggested. It works fine. NO crash here. But when leaving the MapScreen, it lost its state. So did the cameraposition. It returned to the initial location when navigating back. I want it to keep the position as what Google Map and all other map apps do. So, I put the rememberCameraPositionState to the MainActivity.kt. That helps it to survive through navigation. Here is what happened. After that, the CameraPositionState did survive across navigtion. But the app crashed a lot. If I navigate to the other screen, and before that screen is solid, I click the navigation button to get back to MapScreen, the app crashed. Here is the error message.

java.lang.IllegalStateException: CameraPositionState may only be associated with one GoogleMap at a time

I am confused with this error.

How can I fix the problem. I want to keep the state and also no app crash.

Here is the structure of my project. If you need the source code, I'll upload it soon.

MainActivity.kt

class MainActivity : ComponentActivity() {
   ...
   setContent(){
      MayTrail()
      }
}

MyTrail.kt this file only cotains navigation components

fun MyTrail() {
   NavHost() {
   ...
   }
}

MapScreen.kt

@Composable
fun MapScreen() {
   GoogleMap()
}

HistoryScreen.kt

fun HistiryScreen() {
   ...
}

I have tried to test what went wrong and it all points to the rememberCameraPositionState. I saw the problem being repoted. And someone said it is fixed in a former version of maps-compose. But I encouter the problem in the latest version. So is it still a bug? Or I just used it in a wrong way.

Ruineie
  • 23
  • 4

1 Answers1

0

CameraPositionState may only be associated with one GoogleMap at a time

You can control more different GoogleMap with remember1 and remember2, remember3 etc.

Example:

val remember1 = rememberCameraPositionState()
val remember2 = rememberCameraPositionState()
val remember3 = rememberCameraPositionState()
....
GoogleMap(remember1)
GoogleMap(remember2)
Halifax
  • 618
  • 3
  • 9
  • The thing is I only have one map in my project. I don't understand why I hit this error. – Ruineie Mar 24 '23 at 06:42
  • In the GoogleMap source code, there is a setMap method in rememberCameraPositionState. If you repeatedly set the bound map, you will be prompted: CameraPositionState may only be associated with one GoogleMap at a time – Halifax Mar 24 '23 at 07:06
  • Do not break away from the life cycle of your map compose, it is recommended to maintain your rememberCameraPositionState together with map compose – Halifax Mar 24 '23 at 07:07
  • You can also record the position in onDispose separately, then switch back, try to update the position through rememberCameraPositionState – Halifax Mar 24 '23 at 07:08
  • Corret me if I get your point wrong. – Ruineie Mar 24 '23 at 08:37
  • rememberCameraPositionState, for location, map and other information, you can define a remember record position yourself at the activity level you mentioned, and then you can set it to rememberCameraPositionState to restore the state – Halifax Mar 24 '23 at 08:43
  • If you still don't understand, I don't know how to explain it to you in plain language. – Halifax Mar 24 '23 at 08:43
  • Thank you. In the second circumstance. If I navigate slowly between the screens. The app doesn't crash. It crashes if I tap navigation quikly when the mapscreen fades out, and the historyscreen fades in. I'm still confused about that. Isn't the map cleared when I leave the screen. Does this mean the map still exists while fading out. You suggest me to use like DisposableEffect to store the state and then restore it? Did I get your point? – Ruineie Mar 24 '23 at 08:43
  • The default animation of fade in and fade out, you can completely customize and switch animations, for example: slide in and slide out animation horizontally. – Halifax Mar 24 '23 at 08:46
  • If you must force the default fade in and fade out animation, quickly click on the page, emmm, in this case, it is recommended that you use Fragment, and load your ComposeView in each Fragment – Halifax Mar 24 '23 at 08:48
  • Using Fragment can also avoid your fading animation problem – Halifax Mar 24 '23 at 08:49
  • In addition, the default animation duration can also be modified. I have modified it before. You can try to modify it to 0. – Halifax Mar 24 '23 at 08:49
  • Ok, thany you for your patience. I'll revise my code see hwo it works. – Ruineie Mar 24 '23 at 08:51