Skip to content

Android Native — MapLibre Native Android

For Kotlin idioms see ../integrations/kotlin.md.

Install

kotlin
// settings.gradle.kts
dependencyResolutionManagement {
    repositories {
        mavenCentral()
        maven { url = uri("https://maven.maplibre.org") }
    }
}

// app/build.gradle.kts
dependencies {
    implementation("org.maplibre.gl:android-sdk:11.0.0")
}

AndroidManifest.xml

xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- optional -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- optional -->

Register your package name (e.g. com.example.app) in the dashboard against your mfd_pub_* key.

Layout

xml
<!-- res/layout/activity_map.xml -->
<org.maplibre.android.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Activity (Kotlin)

kotlin
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import org.maplibre.android.MapLibre
import org.maplibre.android.camera.CameraPosition
import org.maplibre.android.geometry.LatLng
import org.maplibre.android.maps.MapView

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

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        MapLibre.getInstance(this)
        setContentView(R.layout.activity_map)

        val key      = BuildConfig.MFD_PUB_KEY
        val styleUrl = "https://api.mapsfordevs.com/styles/standard.json?key=$key"

        mapView = findViewById(R.id.mapView)
        mapView.onCreate(savedInstanceState)
        mapView.getMapAsync { map ->
            map.setStyle(styleUrl)
            map.cameraPosition = CameraPosition.Builder()
                .target(LatLng(-26.20, 28.04))
                .zoom(11.0)
                .build()
        }
    }

    override fun onStart()      { super.onStart();      mapView.onStart() }
    override fun onResume()     { super.onResume();     mapView.onResume() }
    override fun onPause()      { super.onPause();      mapView.onPause() }
    override fun onStop()       { super.onStop();       mapView.onStop() }
    override fun onLowMemory()  { super.onLowMemory();  mapView.onLowMemory() }
    override fun onDestroy()    { super.onDestroy();    mapView.onDestroy() }
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState)
    }
}

BuildConfig key

kotlin
// app/build.gradle.kts
android {
    defaultConfig {
        buildConfigField("String", "MFD_PUB_KEY",
            "\"${project.findProperty("MFD_PUB_KEY") ?: ""}\"")
    }
    buildFeatures { buildConfig = true }
}
bash
# ~/.gradle/gradle.properties
MFD_PUB_KEY=mfd_pub_xxxxxxxx

Compose helper

kotlin
import androidx.compose.runtime.Composable
import androidx.compose.ui.viewinterop.AndroidView
import org.maplibre.android.MapLibre
import org.maplibre.android.maps.MapView

@Composable
fun MapLibreMap(styleUrl: String) {
    AndroidView(factory = { ctx ->
        MapLibre.getInstance(ctx)
        MapView(ctx).apply {
            onCreate(null); onStart(); onResume()
            getMapAsync { map -> map.setStyle(styleUrl) }
        }
    })
}

ProGuard / R8

MapLibre ships its own consumer-rules; no extra config needed.