Custom voice controller

Personalize and configure the voice controller in the navigation app using NavigationOptions.

This example shows:

  • How to customize the voice controller and config it using NavigationOptions
documentation image

For all code examples, refer to Navigation Code Examples

CustomVoiceInstructionController view source

1
import UIKit
2
import NbmapNavigation
3
import NbmapCoreNavigation
4
import NbmapDirections
5
import Nbmap
6
7
8
class CustomVoiceInstructionController: UIViewController , VoiceControllerDelegate {
9
10
override func viewDidLoad() {
11
super.viewDidLoad()
12
13
14
let origin = CLLocation(latitude: 37.77440680146262, longitude: -122.43539772352648)
15
let destination = CLLocation(latitude: 37.76556957793795, longitude: -122.42409811526268)
16
let options = NavigationRouteOptions(origin: origin, destination: destination)
17
Directions.shared.calculate(options) { [weak self] routes, error in
18
guard let strongSelf = self else {
19
return
20
}
21
guard error == nil else {
22
print(error!)
23
return
24
}
25
26
guard let routes = routes else { return }
27
28
// Initialize NBNavigationService with the fetched routes, set routeIndex of the primary route to 0
29
let navigationService = NBNavigationService(routes: routes, routeIndex: 0)
30
31
let routeVoiceController = RouteVoiceController()
32
routeVoiceController.voiceControllerDelegate = self
33
34
let navigationOptions = NavigationOptions(navigationService: navigationService,voiceController: routeVoiceController)
35
36
// Initialize the NavigationViewController
37
let navigationViewController = NavigationViewController(for: routes,navigationOptions: navigationOptions)
38
39
// Set the delegate of navigationViewController to subscribe for NavigationView's events, This is optional
40
navigationViewController.modalPresentationStyle = .fullScreen
41
42
// Render part of the route that has been traversed with full transparency, to give the illusion of a disappearing route.
43
navigationViewController.routeLineTracksTraversal = true
44
45
// Start navigation
46
strongSelf.present(navigationViewController, animated: true, completion: nil)
47
}
48
49
}
50
51
func voiceController(_ voiceController: RouteVoiceController, willSpeak instruction: SpokenInstruction, routeProgress: RouteProgress) -> SpokenInstruction? {
52
let modifiedInstruction = SpokenInstruction(distanceAlongStep: instruction.distanceAlongStep, text: instruction.text, ssmlText: instruction.ssmlText, unit: instruction.unit)
53
return modifiedInstruction
54
}
55
}
  • The necessary libraries are imported at the beginning.

    • The UIKit library is for user interface

    • The NbmapNavigation, NbmapCoreNavigation, NbmapDirections, and Nbmap libraries are for the mapping and navigation functionalities.

  • A custom view controller class CustomVoiceInstructionController is defined which inherits from UIViewController and conforms to the VoiceControllerDelegate protocol. This means it is capable of handling user interface-related tasks and can also respond to voice navigation events.

  • The viewDidLoad() function is overridden. This function is called once the view controller's view has been loaded into memory, and it's a good place to set up anything that's needed when your view first appears.

    • Inside viewDidLoad(), the origin and destination points are defined using CLLocation, which represents a geographic coordinate.
  • A NavigationRouteOptions object is created using the origin and destination. This object defines the parameters for a route request.

  • Directions.shared.calculate(options) { ... } is a closure (a function within a function) that calculates the route from origin to destination. The Directions object is a shared instance of the NbmapDirections class, which can be used to calculate directions between locations.

  • If there is no error and the routes are available, a NBNavigationService object is initialized with the routes and the primary route is set to the first route (index 0).

  • A RouteVoiceController object is created and its delegate is set to self, meaning this view controller will handle the voice instructions.

  • A NavigationOptions object is created with the navigationService and voiceController created earlier.

  • Then, a NavigationViewController is initialized with the routes and navigation options. This is a view controller that provides a user interface for turn-by-turn navigation.

  • The modalPresentationStyle of the NavigationViewController is set to .fullScreen, meaning it will take up the entire screen when presented.

  • The routeLineTracksTraversal property is set to true, which means the part of the route that has been traversed will be rendered with full transparency, giving the illusion of a disappearing route.

  • The navigation is started by presenting the NavigationViewController.

  • Lastly, the voiceController(_:willSpeak:routeProgress:) function is defined. This function is called when the voice controller is about to issue a spoken instruction. In this case,

    • it simply returns the original instruction without any modifications.

    • If you want to modify the spoken instructions, you can do so here.

This is a basic explanation of the code, and it's important to note that using location services and mapping in an app requires additional setup, like obtaining necessary permissions from the user and handling different states of the app (like when it's in the background or when the user navigates away from your app).

ios-sdk-7

© 2024 NextBillion.ai all rights reserved.