6

我正在尝试在 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    
    }
}

有人对这个问题有任何想法吗?

PS app id、app code和license key都没有问题。在 AppDelegate 中成功设置了 NMAApplicationContext

4

1 回答 1

5

找到了解决方案!

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

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)
于 2016-09-27T05:29:28.150 回答