Get started
Swift SDK
Show a map

Controls and panels

Controls and panels

MapControls floats the map's controls in one corner, each driving the map. Its toggles open the layers, points-of-interest and preferences panels.

Add the control stack

import MountayaClient
import MountayaElements
import MountayaGeo
import SwiftUI

struct ControlledMap: View {
  let map: MapEngine
  @State private var openPanel: MapPanel?
  @State private var units = UnitPreferences(language: "fr", length: .metric)

  var body: some View {
    MapView(map)
      .ignoresSafeArea()
      .overlay {
        MapControls(map, geolocate: true, pois: true, openPanel: $openPanel, unitPreferences: $units)
      }
  }
}

With nothing stated, the zoom pair, the compass, the layers toggle and the preferences toggle show, top right.

Pick the control for the job

Add your own controls in the stack's trailing closure:

import MountayaElements
import SwiftUI

struct ExportingMap: View {
  let map: MapEngine
  @State private var isExporting = false

  var body: some View {
    MapView(map)
      .overlay {
        MapControls(map) {
          DownloadControl { isExporting = true }
          PanelControl("filters", label: "Filters", glyph: "controls") {
            Text("Huts only")
          }
        }
      }
  }
}

Choose terrains, points of interest and units

The stack's toggles open these panels. Place one yourself where you like:

import MountayaClient
import MountayaElements
import MountayaGeo
import SwiftUI

struct WaterFilters: View {
  let map: MapEngine
  @State private var units = UnitPreferences(language: "fr", length: .metric)

  var body: some View {
    VStack {
      POIPanel(
        map: map,
        groups: [
          POIPanelGroup(id: "water", label: "Water", keys: ["drinking-water"])
        ])
      PreferencesPanel(preferences: $units, map: map)
    }
  }
}

Move through time

Offer the time slider in the layers panel, to draw the snow depth for another hour:

import MountayaElements
import SwiftUI

struct ForecastMap: View {
  let map: MapEngine

  var body: some View {
    MapView(map)
      .overlay { MapControls(map, offersTime: true) }
      .task { try? await map.setOverlay("snow-depth") }
  }
}

A release pins that instant on the map. Back on now, the map follows the clock again.

Next steps

Last updated