0

I'm trying to get Mapbox's in app navigation to work on SwiftUI. Right now viewController returns nil. I've looked at other answers to this question and most of them recommend putting another completion handler in the function where the callback takes place. Here is the code:

 func makeUIViewController(context: Context) -> NavigationViewController {
    getRoute { route in
           let viewController = NavigationViewController(for: route as! Route)
           viewController.modalPresentationStyle = .fullScreen
    }
    //Use of unresolved identifier viewController
    return viewController
}

Adding a closure to this function gives me the error:

Type 'NavView' does not conform to protocol 'UIViewControllerRepresentable'

Aside from adding a closure to the function, what else can I do to get the value out of getRoute{}?

Here is rest of code:

struct NavView: UIViewControllerRepresentable{

typealias CompletionHandler = (Any) -> Void

func makeUIViewController(context: Context) -> NavigationViewController {
    getRoute { route in
           let viewController = NavigationViewController(for: route as! Route)
           viewController.modalPresentationStyle = .fullScreen
    }
    //Use of unresolved identifier viewController
    return viewController
}

func updateUIViewController(_ uiView: NavigationViewController, context: Context) {

}

func getRoute(_ completionHandler: @escaping CompletionHandler){
       // Define two waypoints to travel between
       let origin = Waypoint(coordinate: CLLocationCoordinate2D(latitude: 38.9131752, longitude: -77.0324047), name: "Mapbox")
       let destination = Waypoint(coordinate: CLLocationCoordinate2D(latitude: 38.8977, longitude: -77.0365), name: "White House")

       // Set options
       let options = NavigationRouteOptions(waypoints: [origin, destination])

       // Request a route using MapboxDirections.swift
       Directions.shared.calculate(options) { (waypoints, routes, error) in
          guard let route = routes?.first else { return }
          completionHandler(route)
       }
}
A4_TS
  • 130
  • 1
  • 14
  • 1
    You can’t `return` the value of an asynchronous method, i.e. a method that calls its completion handler later in time. (And don’t be tempted to try to make it synchronous with semaphore or anything silly like that.) This asynchronous retrieval of the route should be moved into the `NavigationViewController` or into the code that initiates this view. But don’t try to bury it within `makeUIViewController`, because that route is calculated asynchronously. – Rob Apr 24 '20 at 20:04
  • @Rob thanks for the response. NavigationViewController comes out of the box from the Mapbox library so I don't think I have access to it. Do you recommend I try anything else? – A4_TS Apr 24 '20 at 20:17
  • @Rob I'll try initializing it somewhere else in the meantime. Thanks – A4_TS Apr 24 '20 at 20:24
  • @Rob Thank you it's solved. Your suggestion helped out a lot – A4_TS Apr 25 '20 at 02:10
  • @A4_TS, can you please let me know how did you initialize NavigationViewController(). Waiting for your response – Programming Learner Jul 20 '21 at 14:55

0 Answers0