Get started

Quickstart

A Jetpack Compose app for Android that shows a topographic map and draws the Tour du Mont-Blanc on it.

Before you start

You need Android Studio with Kotlin 2.4 or newer, and an emulator or device on Android 7.0 (API 24) or later. You also need a publishable key with the tiles and publishing scopes, from Organization → API keys in the Mountaya app.

Install

Create an Empty Activity project in Android Studio. In the app's build.gradle.kts, set compileSdk = 37 and add the modules:

dependencies {
    implementation("com.mountaya:mountaya-client:0.1.0")
    implementation("com.mountaya:mountaya-geo:0.1.0")
    implementation("com.mountaya:mountaya-elements:0.1.0")
}

Declare android.permission.INTERNET in AndroidManifest.xml.

Add your key

Put the key in an .env file beside the app's build script, and list it in .gitignore:

MOUNTAYA_PUBLISHABLE_KEY=pk_your_publishable_key

Hand it to the app as a BuildConfig field, read from the environment, else from .env:

val dotEnv =
    file(".env").takeIf { it.isFile }?.readLines().orEmpty()
        .filter { "=" in it && !it.startsWith("#") }
        .associate { it.substringBefore('=').trim() to it.substringAfter('=').trim() }
val key = System.getenv("MOUNTAYA_PUBLISHABLE_KEY") ?: dotEnv["MOUNTAYA_PUBLISHABLE_KEY"].orEmpty()

android {
    buildFeatures { buildConfig = true }
    defaultConfig { buildConfigField("String", "MOUNTAYA_PUBLISHABLE_KEY", "\"$key\"") }
}

Show the map

Replace MainActivity.kt with this:

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import com.mountaya.client.Credential
import com.mountaya.client.Mountaya
import com.mountaya.elements.MapEngine
import com.mountaya.elements.MapView

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent { TrailMap() }
    }
}

@Composable
fun TrailMap() {
    // One client and one map engine for the screen's lifetime.
    val mountaya = remember { Mountaya(Credential.Publishable(BuildConfig.MOUNTAYA_PUBLISHABLE_KEY)) }
    val map = remember { MapEngine(mountaya) }
    DisposableEffect(map) {
        onDispose {
            map.close()
            mountaya.close()
        }
    }
    LaunchedEffect(map) { map.setTerrain() }
    MapView(map)
}

Draw an itinerary

Replace TrailMap with this version. It draws the Tour du Mont-Blanc and fits the camera to it:

import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import com.mountaya.client.Credential
import com.mountaya.client.Mountaya
import com.mountaya.elements.CameraFit
import com.mountaya.elements.MapEngine
import com.mountaya.elements.MapView
import com.mountaya.geo.PixelPadding

@Composable
fun TrailMap() {
    val mountaya = remember { Mountaya(Credential.Publishable(BuildConfig.MOUNTAYA_PUBLISHABLE_KEY)) }
    val map = remember { MapEngine(mountaya) }
    DisposableEffect(map) {
        onDispose {
            map.close()
            mountaya.close()
        }
    }
    LaunchedEffect(map) {
        map.setTerrain()
        map.setItinerary("0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21", fit = CameraFit(padding = PixelPadding(48.0)))
    }
    MapView(map)
}

Run it

Run the app on an emulator or a device:

./gradlew installDebug

The map opens on the Tour du Mont-Blanc, with the Mountaya watermark in its top-left corner.

Next steps

Last updated