Search code examples
iosswifthere-apihere-ios

NMACoreRouter calculateRouteWithStops no callback (swift)


I'm trying to create a route using Here API in Swift but I'm having some issues because the completion block is never called so I cannot know exactly what is the problem. Here is my code:

let coreRoute = NMACoreRouter()

let startPoint = NMAGeoCoordinates(latitude: latitude1, longitude: longitude1)
let waypoint1 = NMAWaypoint(geoCoordinates: startPoint)
let middlePoint = NMAGeoCoordinates(latitude: latitude2, longitude: longitude2)
let waypoint2 = NMAWaypoint(geoCoordinates: middlePoint, waypointType: NMAWaypointType.ViaWaypoint)
let endPoint = NMAGeoCoordinates(latitude: latitude3, longitude: longitude3)
let waypoint3 = NMAWaypoint(geoCoordinates: endPoint, waypointType: NMAWaypointType.StopWaypoint)

let stopList = [waypoint1, waypoint2, waypoint3] // I have also tried adding the NMAGeoCoordinates to array but still no callback

let routingMode = NMARoutingMode(routingType: NMARoutingType.Fastest, transportMode: NMATransportMode.Car, routingOptions: 0)

coreRoute.calculateRouteWithStops(stopList, routingMode: routingMode) { (routeResult: NMARouteResult?, error: NMARoutingError?) in
    if error == nil && routeResult != nil && routeResult!.routes.count > 0 {
        let route = routeResult!.routes.first as! NMARoute
        let mapRoute = NMAMapRoute(route: route)
        self.mapView.addMapObject(mapRoute)
    } else {
        // Handle error    
    }
}

Does anyone have any idea about this problem?

P.S. There is no problem with the app id, app code and license key. The NMAApplicationContext is successfully set in AppDelegate


Solution

  • Found the solution!

    You need to declare NMACoreRouter object as a class variable.

    class <Class_Name> {
    
        var coreRouter: NMACoreRouter!
    
        func <Your_Function>() {
    
            coreRoute = NMACoreRouter()
    
            let startPoint = NMAGeoCoordinates(latitude: latitude1, longitude: longitude1)
            let waypoint1 = NMAWaypoint(geoCoordinates: startPoint)
            let middlePoint = NMAGeoCoordinates(latitude: latitude2, longitude: longitude2)
            let waypoint2 = NMAWaypoint(geoCoordinates: middlePoint, waypointType: NMAWaypointType.ViaWaypoint)
            let endPoint = NMAGeoCoordinates(latitude: latitude3, longitude: longitude3)
            let waypoint3 = NMAWaypoint(geoCoordinates: endPoint, waypointType: NMAWaypointType.StopWaypoint)
    
            let stopList = [waypoint1, waypoint2, waypoint3] // I have also tried adding the NMAGeoCoordinates to array but still no callback
    
            let routingMode = NMARoutingMode(routingType: NMARoutingType.Fastest, transportMode: NMATransportMode.Car, routingOptions: 0)
    
            coreRoute.calculateRouteWithStops(stopList, routingMode: routingMode) { (routeResult: NMARouteResult?, error: NMARoutingError?) in
                if error == nil && routeResult != nil && routeResult!.routes.count > 0 {
                    let route = routeResult!.routes.first as! NMARoute
                    let mapRoute = NMAMapRoute(route: route)
                    self.mapView.addMapObject(mapRoute)
                } else {
                    // Handle error    
                }
            }      
        }    
    }   
    

    EDIT: Navigation Code

    let navigationManager = NMANavigationManager.sharedNavigationManager()
    navigationManager.delegate = self
    navigationManager.map = mapView
    navigationManager.startTurnByTurnNavigationWithRoute(route)
    navigationManager.startTrackingWithTransportMode(.Car)
    
    //Simulation
    sharedPositioningManager.dataSource = NMARoutePositionSource(route: route)