Get started

Search

SearchControl is a search box over Mountaya's unified search, with its results under it. It searches the visible area of the map first, and frames a result you pick.

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.mountaya.elements.MapEngine
import com.mountaya.elements.MapView
import com.mountaya.elements.SearchControl

@Composable
fun MapWithSearch(map: MapEngine) {
    Box(Modifier.fillMaxSize()) {
        MapView(map, Modifier.fillMaxSize())
        SearchControl(map, Modifier.padding(16.dp))
    }
}

The search reads the Data API through your map's client, so the client needs a session token.

Configure the search

import androidx.compose.runtime.Composable
import com.mountaya.client.generated.DataSchema
import com.mountaya.elements.MapEngine
import com.mountaya.elements.SearchControl
import com.mountaya.geo.SearchLimit
import com.mountaya.geo.UnitPreferences

@Composable
fun FrenchSearch(map: MapEngine) {
    SearchControl(
        map,
        targets = listOf(DataSchema.SearchTarget.Places, DataSchema.SearchTarget.Itineraries, DataSchema.SearchTarget.Zones),
        language = DataSchema.Language.Fr,
        limit = SearchLimit.of(5),
        units = UnitPreferences("fr"),
    )
}

Handle a pick

By default a pick frames the result and marks it on the map. onSelect takes the pick over:

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.SearchControl
import com.mountaya.geo.SearchRow

@Composable
fun SearchThatOpensDetails(map: MapEngine) {
    var picked by remember { mutableStateOf<SearchRow?>(null) }
    SearchControl(map, onSelect = { row -> picked = row })
    picked?.let { BasicText("${it.title}: ${it.secondary}") }
}

Build your own search box

SearchEngine is the search without the box. Feed it the query, and read what a list shows:

import com.mountaya.client.Mountaya
import com.mountaya.client.generated.DataSchema
import com.mountaya.geo.SearchConfiguration
import com.mountaya.geo.SearchEngine
import com.mountaya.geo.SearchFetch
import com.mountaya.geo.SearchFilter
import com.mountaya.geo.searchListState
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.first

suspend fun searchRefuges(scope: CoroutineScope, mountaya: Mountaya) {
    val search =
        SearchEngine(
            scope,
            SearchConfiguration(
                fetch = SearchFetch { input -> mountaya.data.search(input) },
                targets = listOf(DataSchema.SearchTarget.Places, DataSchema.SearchTarget.Itineraries, DataSchema.SearchTarget.Zones),
                language = DataSchema.Language.Fr,
            ),
        )
    search.setQuery("refuge")
    val settled = search.state.first { it.isSettled }
    for (row in searchListState(settled, SearchFilter.Places).rows) println(row.title)
    search.close()
}

SearchControl(map, engine = search) draws your engine in the box instead.

Next steps

Last updated