Skip to main content

Working with the Jetpack Compose Extension

Not all APIs are exposed through the Jetpack Compose Extension, so you can get a reference to the MapView to access the full API surface within a MapEffect. The Mapbox Jetpack Compose Extension wraps the MapView, which allows you to gain this access.

Use MapEffect with caution
Using MapView APIs within a MapEffect can introduce internal state changes that may conflict with Compose states, potentially leading to unexpected or undefined behavior.

The following example showcases how to turn on debug features using MapEffect:

MainActivity.kt
package com.mapbox.maps.demo

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.Modifier
import com.mapbox.maps.debugoptions.MapViewDebugOptions
import com.mapbox.maps.extension.compose.MapEffect
import com.mapbox.maps.extension.compose.MapboxMap

public class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MapboxMap(modifier = Modifier.fillMaxSize()) {
// Get reference to the raw MapView using MapEffect
MapEffect(Unit) { mapView ->
// Use mapView to access the Mapbox Maps APIs not in the Compose extension.
// Changes inside `MapEffect` may conflict with Compose states.
// For example, to enable debug mode:
mapView.debugOptions = setOf(
MapViewDebugOptions.TILE_BORDERS,
MapViewDebugOptions.PARSE_STATUS,
MapViewDebugOptions.TIMESTAMPS,
MapViewDebugOptions.COLLISION,
MapViewDebugOptions.STENCIL_CLIP,
MapViewDebugOptions.DEPTH_BUFFER,
MapViewDebugOptions.MODEL_BOUNDS,
MapViewDebugOptions.TERRAIN_WIREFRAME,
)
}
}
}
}
}
Was this page helpful?