NMACoreRouter calculateRouteWithStops no callback (swift)
Asked Answered
G

1

6

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

Guthrey answered 23/9, 2016 at 9:51 Comment(7)
Are you getting any error messages back at all?Fonda
Nothing. The completion block is never called.Guthrey
Please try to check in the callback the "error" for "NMARoutingError.none" and not for "nil". You won't get nil, so you won't enter this part of your code in your case.Orifice
Thanks, but I've already tried that. The real problem is that the callback is never called. So the debugger does not reach the if-else cases.Guthrey
Which XCode/Swift version, and which version of the SDK do you use ? Could you please try if our Swift example works for you: tcs.ext.here.com/sdk_examples/ExtendedSwiftExample.zip ?Orifice
I use Xcode 7.3.1, Swift 2.2 and SDK 3.2.1. This extended example works very well and it is much more clear with all the comments. I will compare this version and mine to figure out my mistakes. Thanks!Guthrey
I am facing the same issue. Xcode 8.0 Swift 2.3 HERE iOS SDK Premium Edition v3.2.1Goldstone
G
5

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)
Goldstone answered 27/9, 2016 at 5:29 Comment(8)
So the NMACoreRouter object gets released if we declare it within the function and hence the callback is not called.Goldstone
@Burhanuddin Sunelwala, or razvan, do you mind posting a working example of a full page including the class? I have to upgrade from the old Route Manager (that is now deprecated) to the new Core Router and I'm having a tough time. I know that you used to have to provide the delegate to the class as NMARouteManagerDelegate but it's not required anymore I think. Please help me out!Fonda
@ChaimFriedman the above code is the working example! Let us know what specifically you need?Goldstone
Thanks much @Burhanuddin Sunelwala. I figured it out. I must say that they made it much simpler now with CoreRouter than it used to be with the Route Manager. Thanks again and have a nice weekendFonda
@Burhanuddin Sunelwala , I got back to it now. The trouble I am having is sending the configured route to my navigation manager. I know that CoreRouter successfully created a route for me, however the older method I used to use together with the NMARouteManager " navigationManager.startTurnByTurnNavigation(with: route)" does not work for me anymore together with NMACoreRouter. That's why I asked you back then in November if you can show more of your code. Specifically the part you are allocating the route to the navigation manager. ThanksFonda
navigationManager.startTurnByTurnNavigation(with: route) works absolutely fine. Are you implementing the delegate? Are you simulating the navigation for testing?Goldstone
Yes. I was doing all that until now when I used the route manager delegate and everything was working beautifully. Now that I changed from the route manager delegate and switched to the core router the existing navigation manager has stopped working.Fonda
...which is why I asked you if you can post more of the code. Especially the part of sending the route to the navigation manager. In HERE's example posted by @Orifice above there is various stuff added that I am not using until now. For example the NMAPositionManager which I never used (I just use the CLLLocationManager from IOS). That's why I wanted to see what you are doing. ThanksFonda

© 2022 - 2024 — McMap. All rights reserved.