# Search

> Canonical URL: https://docs.mountaya.com/en/sdks/swift/search/
> Last updated: 2026-11-27
> Rules for agents: https://docs.mountaya.com/llms.txt#rules-for-agents

Add a SwiftUI search box over Mountaya's places, itineraries, points of interest and zones, or drive the same search from your own list.

`SearchControl` is a search box over places, ways, points of interest, itineraries, zones and collections. A pick moves the map onto the result and marks it.

## Search places, itineraries and zones

```swift
import MountayaElements
import SwiftUI

struct SearchableMap: View {
  let map: MapEngine
  @State private var query = ""

  var body: some View {
    MapView(map)
      .ignoresSafeArea()
      .overlay(alignment: .top) {
        SearchControl(map: map, query: $query)
          .padding()
      }
  }
}
```

Search reads the Data API through the map's client, so that client needs a session token beside its key ([authentication](https://docs.mountaya.com/en/sdks/swift/authentication.md#rotate)).

- `SearchControl(map:engine:query:isConfined:targets:language:limit:units:wording:fetch:strings:onSelect:onError:)`. Its results show under the box while the focus stands anywhere in the control (its box, its filter chips or its list) over a query of more than white space you have not dismissed, as `isSearchListShown(hasFocus:query:isDismissed:)` answers.
- A round is asked once typing pauses for 150 ms, one request per branch, sections in the order `searchSectionOrder` lists (itineraries, places, ways, points of interest, zones, collections) whatever order they answer in. A new query abandons the round in flight.
- The control starts confined to the map's visible area. A confined round that finds nothing asks once more everywhere, and says so. While the list is open, the camera coming to rest asks the round again.
- A failed branch shows a line of its own, `search.error`, beside its retry, while the others answer: the control's own words, never the API's message, which stays on the branch's `SearchBranchError`.
- The Data API answers a publishable key only beside a session token; without one, search is refused `401`.
- Keep the control below the watermark's frame: `.padding(.top, watermarkClearance + 8)` from `MountayaGeo`, as the sample app does.

## Configure the search

```swift
import MountayaClient
import MountayaElements
import MountayaGeo
import SwiftUI

struct HutSearch: View {
  let map: MapEngine
  @State private var query = ""
  @State private var isConfined = false

  var body: some View {
    SearchControl(
      map: map, query: $query, isConfined: $isConfined,
      targets: [.places, .itineraries, .zones], language: .fr, limit: SearchLimit(5))
  }
}
```

`targets` picks the branches and their order, `limit` the results per branch. `isConfined` binds whether the search keeps to the visible area.

- `targets: [DataSchema.SearchTarget]`: `.itineraries`, `.places`, `.ways`, `.pois`, `.zones`, `.collections`.
- `language: DataSchema.Language?` (`dataLanguageFor("fr-CH")` maps a locale), `limit: SearchLimit?` (`SearchLimit(_:)` refuses a value out of range by answering nil), `units: UnitPreferences?`.
- `wording: SearchWording` words a row's title and second line, your word for each wire value, and your units. `fetch:` sends the requests through a transport of yours in place of the map's client. `strings:` replaces the control's words (the scope's `search` section) by path.
- `onError` is handed each branch's failure; the list shows a line with its retry either way.

## Handle a pick

By default a pick frames the result and draws the map's search highlight. Hand `onSelect` to take the pick over:

```swift
import MountayaElements
import MountayaGeo
import SwiftUI

struct PickingSearch: View {
  let map: MapEngine
  @State private var picked: String?

  var body: some View {
    SearchControl(map: map, onSelect: { row in picked = row.title })
      .overlay(alignment: .bottom) { Text(picked ?? "") }
  }
}
```

- Without `onSelect`, a pick frames the result's extent, else flies to its point, and the map's search highlight marks it: a place as a pin, a point of interest as its category's own, a way as its line, its extent or a pin, in the accent of your tokens. The control takes the highlight down as it leaves.
- A `SearchRow` carries `id`, `title`, `parts` (the title cut at what the query matched, ready to draw), `secondary`, `activities` and `payload` (the result itself). `pickTarget(row.payload)` answers what picking it does to a map, and `map.setSearchHighlight(_:accent:)` marks it from a list of your own.
- With reduced motion on, the pick lands at once rather than easing there.

## Build your own search box

`SearchEngine` is the same search with no view. Feed it the query and read its state:

```swift
import MountayaClient
import MountayaGeo

@MainActor
func searchChamonix(through mountaya: Mountaya) async -> [String] {
  let search = SearchEngine(
    configuration: SearchConfiguration(
      fetch: { try await mountaya.data.search(input: $0) }, language: .fr, limit: SearchLimit(5),
      targets: [.places, .itineraries, .zones]))
  defer { search.close() }
  search.setQuery("Chamonix")
  for await state in search.subscribe() where state.isSettled {
    return searchListState(state, filter: .places).rows.map(\.title)
  }
  return []
}
```

Hand the engine to `SearchControl(map:engine:)` to draw your engine in the standard box.

- `SearchEngine(configuration:query:camera:isOpen:isConfined:pause:refreshPause:sleep:)` waits for the typing to pause, asks one request per branch and keeps the latest round. `setQuery(_:)`, `setConfined(_:)`, `setCamera(_:)` (a stated camera counts as a rest), `configure(_:)` and `retry()` drive it; `state` is its `SearchSnapshot` and `subscribe()` an `AsyncStream` of each.
- An engine starts unconfined unless stated, where the control's own starts confined: state `isConfined: true` on yours to search the visible area.
- A control handed your engine shows and drives it (its typing, its scope and its map's camera), never configures or closes it; your engine's configuration stands in for `targets`, `language`, `limit`, `wording` and `fetch`.
- The list rules are Geo's: `searchListState(_:filter:)`, `sectionsForFilter(_:filter:)`, the row builders (`placeRow`, `poiRow`, `wayRow`, `itineraryRow`, `zoneRow`, `collectionRow`), `highlightParts(_:query:)`.

## Next steps

- [Show an elevation profile](https://docs.mountaya.com/en/sdks/swift/elevation-profile.md): The itinerary's curve under the map.
- [Geo without a map](https://docs.mountaya.com/en/sdks/swift/geo.md): Distances, units and terrain with no network.
