In this page

Custom Waypoint arrival screen

这篇文档目前尚未提供译文,将以原文展示。

Customize the arrival experience to enhance user engagement and control during their navigation journey.

This example shows:

  1. How to customize the arrival screen when arriving at a waypoint of the route

  2. In the example code, arrival screen will show the waypoint name and user could choose to continue navigating to next waypoint

For all code examples, refer to Navigation Code Examples

CustomWaypointScreenController view source

1import UIKit
2import NbmapNavigation
3import NbmapCoreNavigation
4import NbmapDirections
5
6class CustomWaypointScreenController: UIViewController {
7  
8   override func viewDidLoad() {
9       super.viewDidLoad()
10      
11       let origin = CLLocation(latitude: 37.77440680146262, longitude: -122.43539772352648)
12       let waypoint = CLLocation(latitude: 37.76856957793795, longitude: -122.42709811526268)
13       let destination = CLLocation(latitude: 37.76556957793795, longitude: -122.42409811526268)
14       let options = NavigationRouteOptions(locations: [origin,waypoint,destination])
15       Directions.shared.calculate(options) { [weak self] routes, error in
16           guard let strongSelf = self else {
17               return
18           }
19           guard error == nil else {
20               print(error!)
21               return
22           }
23          
24           guard let routes = routes else { return }
25          
26           let navigationService = NBNavigationService(routes: routes, routeIndex: 0, simulating : simulationIsEnabled ? SimulationMode.always : SimulationMode.onPoorGPS)
27          
28           let navigationOptions = NavigationOptions(navigationService: navigationService)
29          
30           let navigationViewController = NavigationViewController(for: routes,navigationOptions: navigationOptions)
31           navigationViewController.modalPresentationStyle = .fullScreen
32           navigationViewController.delegate = self
33          
34           // Start navigation
35           strongSelf.present(navigationViewController, animated: true, completion: nil)
36       }
37   }
38}
39
40extension CustomWaypointScreenController : NavigationViewControllerDelegate {
41   func navigationViewController(_ navigationViewController: NavigationViewController, didArriveAt waypoint: Waypoint) -> Bool {
42       let isFinalLeg = navigationViewController.navigationService.routeProgress.isFinalLeg
43       if isFinalLeg {
44           return true
45       }
46      
47       let alert = UIAlertController(title: "Arrived at \(waypoint.name ?? "Unknown").", message: "Would you like to continue?", preferredStyle: .alert)
48       alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { _ in
49           // Begin the next leg once the driver confirms
50           guard !isFinalLeg else { return }
51           navigationViewController.navigationService.router.routeProgress.legIndex += 1
52           navigationViewController.navigationService.start()
53       }))
54       navigationViewController.present(alert, animated: true, completion: nil)
55      
56       return false
57   }
58}

This class is a view controller that allows users to navigate to a destination with waypoints. The class has the following properties:

  1. origin: The origin location of the navigation.

  2. waypoint: The waypoint location of the navigation.

  3. destination: The destination location of the navigation.

  4. routes: An array of Route objects that represent the routes between the origin, waypoint, and destination.

  5. navigationService: A NBNavigationService object that is used to control the navigation.

  6. navigationViewController: A NavigationViewController object that is used to display the navigation.

The class has the following methods:

  1. viewDidLoad(): This method is called when the view controller is loaded. In this method, the navigation service is created and configured.

  2. didArriveAtWaypoint(): This method is called when the navigation service arrives at a waypoint. In this method, an alert is presented to the user asking if they would like to continue to the next waypoint.

The code you provided also has an extension:

  1. CustomWaypointScreenController: NavigationViewControllerDelegate

This extension conforms the CustomWaypointScreenController class to the NavigationViewControllerDelegate protocol. This protocol allows the CustomWaypointScreenController class to respond to events from the navigation controller.

ios-sdk-7
没找到你要找的内容?