Bring your own route

Dynamically update and adjust selected routes based on user preferences and real-time conditions.

This example shows:

  • How to customize your RoutingProvider for NBNavigationSerivce

  • How to simulate your routing Progress with customized NavigatinOptions

docs-image

For all code examples, refer to Navigation Code Examples

CustomRouteNavigationController view source

1
import UIKit
2
import NbmapNavigation
3
import NbmapCoreNavigation
4
import NbmapDirections
5
6
class CustomRouteNavigationController: UIViewController , NGLMapViewDelegate {
7
8
override func viewDidLoad() {
9
super.viewDidLoad()
10
let origin = CLLocation(latitude: 37.77440680146262, longitude: -122.43539772352648)
11
let destination = CLLocation(latitude: 37.76556957793795, longitude: -122.42409811526268)
12
let options = NavigationRouteOptions(origin: origin, destination: destination)
13
14
let customRoutingProvider = CustomRoutingProvider()
15
_ = customRoutingProvider.calculateRoutes(options: 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,routingProvider: customRoutingProvider)
27
28
let navigationOptions = NavigationOptions(navigationService: navigationService)
29
let navigationViewController = NavigationViewController(for: routes,navigationOptions: navigationOptions)
30
navigationViewController.modalPresentationStyle = .fullScreen
31
strongSelf.present(navigationViewController, animated: true, completion: nil)
32
}
33
34
}
35
}
36
37
public struct CustomRequest: NavigationProviderRequest {
38
/**
39
Cancels the request if it is still active.
40
*/
41
public func cancel() {
42
dataTask.cancel()
43
}
44
45
var dataTask : URLSessionDataTask
46
}
47
48
class CustomRoutingProvider : RoutingProvider {
49
func calculateRoutes(options: NbmapDirections.RouteOptions, completionHandler: @escaping NbmapDirections.Directions.RouteCompletionHandler) -> NbmapCoreNavigation.NavigationProviderRequest? {
50
let task = Directions.shared.calculate(options, completionHandler: completionHandler)
51
return CustomRequest(dataTask: task)
52
53
}
54
}

The code above creates a simple custom routing app using NbmapNavigation libraries. It consists of two classes: CustomRouteNavigationController and CustomRoutingProvider.

The CustomRouteNavigationController class is the main view controller that sets up the navigation:

  • It sets the origin and destination coordinates for the route.

  • It creates a NavigationRouteOptions object using the origin and destination.

  • It initializes a CustomRoutingProvider to provide custom routing services.

  • It calls the calculateRoutes function from the custom routing provider, passing the options and a completion handler.

  • When a route is received in the completion handler, it initializes a NBNavigationService with the calculated routes and settings.

  • It creates a NavigationOptions object with the navigationService.

  • It initializes a NavigationViewController and presents it.

The CustomRoutingProvider class is a custom implementation of the RoutingProvider protocol that uses NbmapDirections to calculate routes:

  • It implements the calculateRoutes function that takes a RouteOptions object and a completion handler.

  • It calls the calculate function from Directions.shared and returns a CustomRequest object with the URLSessionDataTask object.

© 2024 NextBillion.ai all rights reserved.