Get started
Swift SDK
Geo and navigation

Turn-by-turn navigation

Turn-by-turn navigation

MountayaNavigation follows a route on the device: it reads your position, says each maneuver in your words, and tells you what is left until you arrive.

Read the route

A saved itinerary is followed whole, as one route with its steps:

import MountayaClient
import MountayaGeo

struct NoRoute: Error {}

// Reads the Tour du Mont-Blanc into the route an engine follows, or throws where it has none.
func readRoute(through mountaya: Mountaya) async throws -> NavigationRoute {
  let itinerary = try await mountaya.studio.itineraries.getItinerary(
    itinerary_id: "0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21")
  guard let route = readItineraryRoute(itinerary.data.segments, waypoints: itinerary.data.waypoints)
  else { throw NoRoute() }
  return route
}

Follow the route

An engine takes a route source and the positions you hand it. RouteFollowingEngine is the engine with no device behind it:

import MountayaClient
import MountayaGeo

struct NoRoute: Error {}

@MainActor
func follow(through mountaya: Mountaya) async throws {
  let navigation = RouteFollowingEngine<String>(route: { id in
    let itinerary = try await mountaya.studio.itineraries.getItinerary(itinerary_id: id)
    guard
      let route = readItineraryRoute(itinerary.data.segments, waypoints: itinerary.data.waypoints)
    else { throw NoRoute() }
    return route
  })
  navigation.start("0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21")

  // Hand positions once the route has loaded: here, the start in Chamonix.
  for await state in navigation.subscribe() where state.status == .navigating {
    try navigation.setPosition([6.8712, 45.9231])
    print(navigation.state.progress?.toArrival.distance ?? 0, "meters to go")
    break
  }
}

state is observable, so a SwiftUI view that reads it is drawn again as you move.

Use the device's location

DeviceNavigationEngine reads the device's position while your app holds the permission. Your app asks for it, and says why in Info.plist:

<key>NSLocationWhenInUseUsageDescription</key>
<string>Your position on the map, and along the route you follow.</string>
import CoreLocation
import MountayaClient
import MountayaGeo
import MountayaNavigation

struct NoRoute: Error {}

@MainActor
func navigate(through mountaya: Mountaya, access: CLLocationManager) -> DeviceNavigationEngine<String>
{
  access.requestWhenInUseAuthorization()
  let navigation = DeviceNavigationEngine<String>(
    route: { id in
      let itinerary = try await mountaya.studio.itineraries.getItinerary(itinerary_id: id)
      guard
        let route = readItineraryRoute(itinerary.data.segments, waypoints: itinerary.data.waypoints)
      else { throw NoRoute() }
      return route
    },
    location: DeviceLocationSource(rate: .walking))
  navigation.start("0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21")
  return navigation
}

Speak the prompts

SpeechNavigationVoice says your sentence for each instruction, in the language you name:

import MountayaGeo
import MountayaNavigation

@MainActor
func frenchVoice() -> SpeechNavigationVoice {
  // Your words for each instruction. One you answer nil for is not said.
  SpeechNavigationVoice(language: "fr-FR") { instruction in
    switch instruction.key {
    case .turnLeft: "Tournez à gauche sur \(instruction.name ?? "")"
    case .turnLeftPlain: "Tournez à gauche"
    case .waypoint: "Passez par \(instruction.name ?? "")"
    case .goal, .goalPlain: "Vous êtes arrivé"
    default: nil
    }
  }
}

Hand it to DeviceNavigationEngine(voice:). The engine says each maneuver once, 50 m before it.

Show progress

state.progress says how far it is to the next maneuver and to the arrival:

import MountayaGeo
import MountayaNavigation
import SwiftUI

struct Progress: View {
  let navigation: DeviceNavigationEngine<String>
  let units = UnitPreferences(language: "en")

  var body: some View {
    if let progress = navigation.state.progress {
      Text("\(formatDistance(progress.toManeuver.distance, units: units)) to the next turn")
      Text("\(formatDistance(progress.toArrival.distance, units: units)) to go")
    }
  }
}

Replay a route

ReplayLocationSource walks the route at a speed you name, for a demo or a test on the simulator:

import MountayaGeo
import MountayaNavigation

@MainActor
func demo(_ route: NavigationRoute) -> DeviceNavigationEngine<NavigationRoute> {
  let navigation = DeviceNavigationEngine<NavigationRoute>(
    route: { $0 }, location: ReplayLocationSource(metersPerSecond: 10))
  navigation.start(route)
  return navigation
}

Next steps

Last updated