Get started
Kotlin SDK

Show a map

Show a map

MapEngine holds what the map shows, on every target. On Android, MapView draws it in Compose over MapLibre Native.

Render the map

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import com.mountaya.client.Mountaya
import com.mountaya.elements.MapEngine
import com.mountaya.elements.MapView

@Composable
fun TrailMap(mountaya: Mountaya) {
    val map = remember { MapEngine(mountaya) }
    LaunchedEffect(map) { map.setTerrain() }
    MapView(map, Modifier.fillMaxSize())
}

Draw an itinerary

import com.mountaya.elements.CameraFit
import com.mountaya.elements.MapEngine
import com.mountaya.geo.PixelPadding

suspend fun drawTour(map: MapEngine) {
    map.setItinerary("0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21", fit = CameraFit(padding = PixelPadding(48.0)))
    // The zones of your credential's scope, beside it.
    map.setZones(true)
}

Call it from a LaunchedEffect. A later call replaces the itinerary.

Move the camera

import com.mountaya.elements.CameraFit
import com.mountaya.elements.CameraTarget
import com.mountaya.elements.MapEngine
import com.mountaya.geo.PixelPadding

fun showChamonix(map: MapEngine) {
    // A move states the members it changes; the bearing and the pitch are kept.
    map.ease(CameraTarget(center = listOf(6.8712, 45.9231), zoom = 12.0))
    // The box around Chamonix and Argentière: west, south, east, north.
    map.fitBounds(listOf(6.80, 45.85, 7.00, 46.00), CameraFit(padding = PixelPadding(48.0)))
    map.resetNorth()
}

Respond to the user

onClick hands you each click and the marker it fell on. onCameraChange hands you the camera each time it comes to rest:

import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import com.mountaya.elements.MapEngine
import com.mountaya.elements.MapView
import com.mountaya.geo.CameraPose

@Composable
fun PickingMap(map: MapEngine) {
    var picked by remember { mutableStateOf<String?>(null) }
    var camera by remember { mutableStateOf<CameraPose?>(null) }
    MapView(map, onClick = { tap -> picked = tap.marker }, onCameraChange = { camera = it })
    BasicText("Marker ${picked ?: "none"} at zoom ${camera?.zoom}")
}

Kotlin names a tap on the map a click: onClick, MapEvent.Click.

Hear what the map refuses

A key without the tiles scope, or an agreement that does not cover the map, refuses the map's own requests. The engine says so:

import com.mountaya.elements.MapEngine
import com.mountaya.elements.MapEvent

suspend fun printErrors(map: MapEngine) {
    map.events().collect { event ->
        when (event) {
            is MapEvent.AccessRefused -> println("refused: ${event.status} at ${event.url}")
            is MapEvent.Failure -> println("failed: ${event.status} at ${event.url}, ${event.cause}")
            else -> Unit
        }
    }
}

map.state.value.isAccessRequired stays true while a refusal stands, so a notice you raise on it comes down by itself.

Read a point

A long press opens the readout: what the map draws there, the terrain's altitude, slope and aspect, and the coordinates. Turn it off with readout = false:

import androidx.compose.runtime.Composable
import com.mountaya.elements.MapEngine
import com.mountaya.elements.MapView

@Composable
fun MapWithoutReadout(map: MapEngine) {
    MapView(map, readout = false)
}

Attribution and watermark

The map shows the Mountaya watermark in its top-left corner unless your organization holds white label, and lists its credits, Mountaya first. Keep your own views clear of the watermark's frame:

import com.mountaya.elements.MapEngine
import com.mountaya.geo.isWatermarkKeptClear
import com.mountaya.geo.watermarkClearance

fun clearance(map: MapEngine): String {
    val keepsClear = isWatermarkKeptClear(map.organizationRead.value)
    return "Keep clear: $keepsClear, below $watermarkClearance dp. Credits: ${map.credits}"
}

Place waypoints

An edit places a waypoint at each click, on the line where the click falls within 40 dp of an active segment. Move one of the Tour du Mont-Blanc's waypoints:

import com.mountaya.elements.MapEngine
import com.mountaya.elements.WaypointEdit

suspend fun startMoving(map: MapEngine, route: List<List<Double>>) {
    map.setItinerary("0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21")
    // The itinerary's line, which a click near it snaps to.
    map.setActiveSegments(listOf(route))
    map.beginWaypointEdit("5f2b8c1e-7a4d-4e9b-b3c6-0d8e1f2a4b67")
}

fun finishMoving(map: MapEngine): WaypointEdit? = map.endWaypointEdit()

MapView places the waypoint at each click while the edit runs.

Next steps

Last updated