MapViewState
MapViewState is the core component that manages map initialization, camera position, and overall map state. Each map provider has its own implementation while maintaining a consistent interface.
Provider Implementations
Section titled “Provider Implementations”MapConductor supports four map providers, each with their own MapViewState implementation:
GoogleMapViewStateImpl- Google MapsMapboxViewStateImpl- Mapbox MapsHereViewStateImpl- HERE MapsArcGISMapViewStateImpl- ArcGIS Maps
Core Properties
Section titled “Core Properties”Initialization State
Section titled “Initialization State”isInitialized: StateFlow<InitState>: Tracks the map initialization statusNotStarted: Map initialization has not begunInitializing: Map is currently being initializedInitialized: Map is ready to useFailed: Initialization failed
Camera Management
Section titled “Camera Management”cameraPosition: StateFlow<MapCameraPositionImpl?>: Current camera positioninitCameraPosition: MapCameraPositionImpl: Initial camera position when map loads
Map Design
Section titled “Map Design”mapDesignType: ActualMapDesignType: Map style/design (provider-specific)
Core Methods
Section titled “Core Methods”Initialization
Section titled “Initialization”fun initAsync(init: suspend () -> Boolean)Initializes the map asynchronously. Returns true if successful.
fun resetInitState()Resets the initialization state to NotStarted.
Camera Movement
Section titled “Camera Movement”fun moveCameraTo( cameraPosition: MapCameraPositionImpl, durationMills: Long? = 0, listener: MoveCameraCallback? = null)Moves the camera to a specific position with optional animation.
fun moveCameraTo( position: GeoPointImpl, durationMills: Long? = 0, listener: MoveCameraCallback? = null)Moves the camera to focus on a specific geographic point.
Usage Example
Section titled “Usage Example”Basic Setup
Section titled “Basic Setup”@Composablefun MapExample() { // Create map state (choose your provider) val mapViewState = rememberGoogleMapViewState()
// Monitor initialization state val initState by mapViewState.isInitialized.collectAsState()
when (initState) { InitState.NotStarted -> Text("Map not started") InitState.Initializing -> CircularProgressIndicator() InitState.Initialized -> { // Replace MapView with your chosen map provider, such as GoogleMapView, MapboxMapViewMapView(state = mapViewState) { // Add map content here } } InitState.Failed -> Text("Map failed to load") }}Camera Control
Section titled “Camera Control”@Composablefun CameraControlExample() { val mapViewState = rememberGoogleMapViewState()
Column { Button( onClick = { val sanFrancisco = GeoPointImpl.fromLatLong(37.7749, -122.4194) mapViewState.moveCameraTo( position = sanFrancisco, durationMills = 1000, listener = object : MapViewState.MoveCameraCallback { override fun onComplete() { println("Camera movement completed") } } ) } ) { Text("Move to San Francisco") }
// Replace MapView with your chosen map provider, such as GoogleMapView, MapboxMapViewMapView(state = mapViewState) { // Map content } }}Provider Switching
Section titled “Provider Switching”@Composablefun ProviderSwitchExample() { var selectedProvider by remember { mutableStateOf("google") }
val mapViewState = remember(selectedProvider) { when (selectedProvider) { "google" -> GoogleMapViewStateImpl() "mapbox" -> MapboxViewStateImpl() "here" -> HereViewStateImpl() "arcgis" -> ArcGISMapViewStateImpl() else -> GoogleMapViewStateImpl() } }
Column { Row { Button(onClick = { selectedProvider = "google" }) { Text("Google Maps") } Button(onClick = { selectedProvider = "mapbox" }) { Text("Mapbox") } Button(onClick = { selectedProvider = "here" }) { Text("HERE") } Button(onClick = { selectedProvider = "arcgis" }) { Text("ArcGIS") } }
// Replace MapView with your chosen map provider, such as GoogleMapView, MapboxMapViewMapView(state = mapViewState) { Marker( position = GeoPointImpl.fromLatLong(37.7749, -122.4194), icon = DefaultIcon(label = selectedProvider) ) } }}Event Handling
Section titled “Event Handling”The MapViewState works with your chosen map provider component to provide comprehensive event handling:
// Replace MapView with your chosen map provider, such as GoogleMapView, MapboxMapViewMapView( state = mapViewState, onMapViewInitialized = { println("Map view initialized") }, onMapLoaded = { println("Map loaded successfully") }, onMapClick = { geoPoint -> println("Map clicked at: ${geoPoint.latitude}, ${geoPoint.longitude}") }) { // Map content}Best Practices
Section titled “Best Practices”- Remember State: Always use
rememberto maintain state across recompositions - Monitor Initialization: Check initialization state before adding content
- Handle Failures: Provide fallback UI for initialization failures
- Provider Abstraction: Write code that works with any provider implementation
- Resource Management: Allow the SDK to handle lifecycle management automatically