NMACoreRouter calculateRouteWithStops 无回调 (swift)

NMACoreRouter calculateRouteWithStops no callback (swift)

我正在尝试使用 Swift 中的 Here API 创建路由,但我遇到了一些问题,因为从未调用完成块,所以我无法确切知道问题出在哪里。 这是我的代码:

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    
    }
}

有人知道这个问题吗?

P.S。 app id、app code和license key都没有问题。在 AppDelegate

中成功设置了 NMAApplicationContext

找到解决方案!

您需要将 NMACoreRouter 对象声明为 class 变量。

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    
            }
        }      
    }    
}   

编辑:导航代码

let navigationManager = NMANavigationManager.sharedNavigationManager()
navigationManager.delegate = self
navigationManager.map = mapView
navigationManager.startTurnByTurnNavigationWithRoute(route)
navigationManager.startTrackingWithTransportMode(.Car)

//Simulation
sharedPositioningManager.dataSource = NMARoutePositionSource(route: route)