Get started

Search

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.

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).

Configure the search

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.

Handle a pick

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

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 ?? "") }
  }
}

Build your own search box

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

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.

Next steps

Last updated