Get started
Swift SDK

Show a map

Show a map

A map is a MapEngine that holds what the map shows, and a SwiftUI MapView that draws it on iPhone and iPad. Turn on the Elements trait first (install).

Render the map

import MountayaClient
import MountayaElements
import SwiftUI

struct TrailMap: View {
  @State private var map: MapEngine

  init(mountaya: Mountaya) {
    _map = State(initialValue: MapEngine(client: mountaya))
  }

  var body: some View {
    MapView(map)
      .ignoresSafeArea()
      .task { try? await map.setTerrain() }
  }
}

The map fetches its style and tiles with your client's key. setTerrain() shows the first terrain of your catalogue.

Draw an itinerary

import MountayaElements
import MountayaGeo

@MainActor
func showTour(on map: MapEngine) async throws {
  try await map.setItinerary(
    "0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21", fit: CameraFit(padding: PixelPadding(48)))
  try await map.setZones(true)
}

A later call replaces the itinerary drawn. Cancelling the task withdraws a call still loading.

Move the camera

import MountayaElements
import MountayaGeo

@MainActor
func frameChamonix(on map: MapEngine) throws {
  try map.ease(to: CameraTarget(center: [6.8712, 45.9231], zoom: 12))
  // Chamonix to Argentière: west, south, east, north.
  try map.fitBounds([6.80, 45.85, 7.00, 46.00], with: CameraFit(padding: PixelPadding(48)))
  map.resetNorth()
}

Respond to the user

MapView hands you each tap and each camera at rest:

import MountayaElements
import MountayaGeo
import SwiftUI

struct TappableMap: View {
  let map: MapEngine
  @State private var picked: String?
  @State private var zoom = 0.0

  var body: some View {
    MapView(
      map,
      onTap: { tap in picked = tap.marker },
      onCameraChange: { camera in zoom = camera.zoom }
    )
    .overlay(alignment: .bottom) { Text(picked ?? "Zoom \(Int(zoom))") }
  }
}

For what lands under a tap, read the map's events:

import MountayaElements

@MainActor
func listen(to map: MapEngine) async {
  for await event in map.events() {
    if case .featureTap(_, let features) = event {
      print(features.map(\.layer))
    }
  }
}

Hear what the map refuses

A request the map's own loader cannot complete reaches its events. Listen for them and log each:

import MountayaElements

@MainActor
func logRefusals(of map: MapEngine) async {
  for await event in map.events() {
    switch event {
    case .accessRefused(let status, let url):
      print("refused \(status): \(url)")
    case .failure(let url, let status, let cause):
      print("failed \(status.map(String.init) ?? "with no answer"): \(url)", cause ?? "")
    default:
      break
    }
  }
}

While a refusal stands, map.state.isAccessRequired is true: show your own notice over the map.

Read a point

A long press opens a readout of the point: the terrain's altitude, slope and aspect, what is drawn there, and the coordinates. Turn it off with MapView(map, readout: false); the long press still reaches events().

Attribution and watermark

The map shows the Mountaya watermark in the top-left corner of its safe area, unless your organization holds white label. Keep your own views clear of it:

import MountayaGeo

// Where a bar of yours across the top starts, as the map's read of your organization stands.
func barTop(safeAreaTop top: Double, read: OrganizationRead) -> Double {
  isWatermarkKeptClear(read) ? top + watermarkClearance + 8 : top
}

The credit button lists the map's sources, Mountaya first.

Place waypoints

Begin an edit, and each tap places the waypoint. A tap near the itinerary's line snaps onto it:

import MountayaElements

@MainActor
func moveWaypoint(on map: MapEngine, along line: [[Double]]) async throws {
  try await map.setItinerary("0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21")
  map.setActiveSegments([line])
  try map.beginWaypointEdit("5f2b8c1e-7a4d-4e9b-b3c6-0d8e1f2a4b67")
}

// Once the reader has tapped where it goes: where the waypoint now stands.
@MainActor
func finishEdit(on map: MapEngine) -> [Double]? {
  map.endWaypointEdit()?.position
}

Next steps

Last updated