Get started

Quickstart

A SwiftUI app for iPhone that shows a topographic map and draws the Tour du Mont-Blanc on it.

Before you start

You need Xcode with Swift 6.2.3 or newer, an iOS 17 simulator or device, and a publishable key with the tiles and publishing scopes from Organization → API keys in the Mountaya app.

Install

Create an iOS App project in Xcode with SwiftUI. Choose File → Add Package Dependencies, paste https://github.com/mountaya/sdk-swift, and add MountayaClient, MountayaGeo and MountayaElements to the app target. Then turn on the package's Elements trait under Package Dependencies in the project settings.

Add your key

Add a Local.xcconfig that git ignores, set it as the target's configuration file, and pass the key to Info.plist:

MOUNTAYA_PUBLISHABLE_KEY = pk_your_publishable_key
<key>MOUNTAYA_PUBLISHABLE_KEY</key>
<string>$(MOUNTAYA_PUBLISHABLE_KEY)</string>

Show the map

Replace the app's Swift file with this:

import Foundation
import MountayaClient
import MountayaElements
import SwiftUI

@main
struct TrailApp: App {
  @State private var map = TrailApp.makeMap()

  var body: some Scene {
    WindowGroup {
      if let map {
        TrailMap(map: map)
      } else {
        Text("Set MOUNTAYA_PUBLISHABLE_KEY in Local.xcconfig.")
      }
    }
  }

  // A map engine over a client that holds the key from Info.plist.
  static func makeMap() -> MapEngine? {
    let key = Bundle.main.object(forInfoDictionaryKey: "MOUNTAYA_PUBLISHABLE_KEY") as? String
    guard let key, let mountaya = try? Mountaya(credential: .publishable(key: key)) else {
      return nil
    }
    return MapEngine(client: mountaya)
  }
}

struct TrailMap: View {
  let map: MapEngine

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

MapEngine holds what the map shows, and MapView draws it.

Draw an itinerary

Replace TrailMap with this version. It draws the itinerary and fits the camera to it:

import MountayaElements
import MountayaGeo
import SwiftUI

struct TrailMap: View {
  let map: MapEngine

  var body: some View {
    MapView(map)
      .ignoresSafeArea()
      .task {
        try? await map.setTerrain()
        _ = try? await map.setItinerary(
          "0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21", fit: CameraFit(padding: PixelPadding(48)))
      }
  }
}

Run it

Run the app on an iPhone simulator. The map opens on the Tour du Mont-Blanc, with the Mountaya watermark in its top-left corner.

Next steps

Last updated