Skip to main content

Overview

The MapView is the main UI element for displaying maps in Open Mobile Maps on Android. It handles map rendering, layer management, and camera control.

Initialization

Before using the MapView, initialize the MapsCore library:
MapsCore.initialize()
Call this as early as possible, typically in your Application’s onCreate() method.

Setup

Basic MapView Setup

After retrieving a reference to the MapView, set it up with a coordinate system and register it to a lifecycle:
mapView.setupMap(MapConfig(CoordinateSystemFactory.getEpsg3857System()))
mapView.registerLifecycle(lifecycle)
MapConfig
MapConfig
required
Configuration object that specifies the coordinate system for the camera. Common default is EPSG 3857 (Web Mercator).

setupMap()

Initializes the map with the specified configuration.
fun setupMap(config: MapConfig)
config
MapConfig
required
The map configuration containing the coordinate system identifier

registerLifecycle()

Registers the MapView to an Android lifecycle for proper resource management.
fun registerLifecycle(lifecycle: Lifecycle)
lifecycle
Lifecycle
required
The lifecycle to register the MapView to (typically from an Activity or Fragment)

Layer Management

addLayer()

Adds a layer to the MapView for rendering.
fun addLayer(layer: LayerInterface)
layer
LayerInterface
required
The layer to add to the map (e.g., TiledRasterLayer, PolygonLayerInterface, IconLayerInterface)
Example - Adding a Tiled Raster Layer:
val tiledRasterLayer = TiledRasterLayer()
mapView.addLayer(tiledRasterLayer)
Example - Adding a Tiled Vector Layer:
val tiledVectorLayer = TiledVectorLayer(context, "https://www.sample.org/base-map/style.json")
mapView.addLayer(tiledVectorLayer)

Camera Access

getCamera()

Returns the camera interface for controlling the map view.
fun getCamera(): Camera2dInterface
Returns: Camera2dInterface - The camera controller for the map Example:
mapView.getCamera().moveToCenterPositionZoom(
    Coord(CoordinateSystemIdentifiers.EPSG4326(), 8.378232525377973, 46.962592372639634, 0.0),
    10000000.0,
    false
)

MapView State

The MapView provides a mapViewState StateFlow that communicates its current state. This is important for ensuring the map is ready before performing operations that require proper view bounds computation.
val mapViewState: StateFlow<MapViewState>

Complete Example

class MapActivity : AppCompatActivity() {
    private lateinit var mapView: MapView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        // Initialize the library
        MapsCore.initialize()
        
        // Setup MapView
        mapView.setupMap(MapConfig(CoordinateSystemFactory.getEpsg3857System()))
        mapView.registerLifecycle(lifecycle)
        
        // Add a raster layer
        val dataLoader = DataLoader(this, cacheDir, 50L * 1024L * 1024L, "example-referrer")
        val tiledRasterLayer = TiledRasterLayer()
        mapView.addLayer(tiledRasterLayer)
        
        // Set camera position
        mapView.getCamera().moveToCenterPositionZoom(
            Coord(CoordinateSystemIdentifiers.EPSG4326(), 8.378232, 46.962592, 0.0),
            10000000.0,
            false
        )
    }
}

Build docs developers (and LLMs) love