Display a map
This example demonstrates the usage of the MapboxMap
in a Compose environment. The map is configured using the rememberMapViewportState
to set specific camera options such as zoom level and center location. In this case, the app sets the initial zoom level to 9.0 using the constant ZOOM
and centers the map on the coordinates for Helsinki, obtained from the CityLocations
utility class.
The map is displayed within the fillMaxSize
layout modifier to occupy the entire available space on the screen. The entire layout is themed using MapboxMapComposeTheme
, a theme used in the Maps SDK for Android Examples App.
This example code is part of the Maps SDK for Android Examples App, a working Android project available on GitHub. Android developers are encouraged to run the examples app locally to interact with this example in an emulator and explore other features of the Maps SDK.
See our Run the Maps SDK for Android Examples App tutorial for step-by-step instructions.
package com.mapbox.maps.compose.testapp.examples.basic
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.compose.testapp.ExampleScaffold
import com.mapbox.maps.compose.testapp.examples.utils.CityLocations
import com.mapbox.maps.compose.testapp.ui.theme.MapboxMapComposeTheme
import com.mapbox.maps.extension.compose.MapboxMap
import com.mapbox.maps.extension.compose.animation.viewport.rememberMapViewportState
/**
* Example to showcase usage of MapboxMap.
*/
public class SimpleMapActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MapboxMapComposeTheme {
ExampleScaffold {
MapboxMap(
Modifier.fillMaxSize(),
mapViewportState = rememberMapViewportState {
setCameraOptions {
zoom(ZOOM)
center(CityLocations.HELSINKI)
}
},
)
}
}
}
}
private companion object {
const val ZOOM: Double = 9.0
}
}