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

Burhanuddin Sunelwala
  • 5,318
  • 3
  • 25
  • 51
razvan
  • 133
  • 4
  • Are you getting any error messages back at all? – Chaim Friedman Sep 25 '16 at 23:16
  • Nothing. The completion block is never called. – razvan Sep 26 '16 at 03:22
  • 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. – Marco Sep 26 '16 at 07:57
  • 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. – razvan Sep 26 '16 at 09:12
  • 1
    Which XCode/Swift version, and which version of the SDK do you use ? Could you please try if our Swift example works for you: https://tcs.ext.here.com/sdk_examples/ExtendedSwiftExample.zip ? – Marco Sep 26 '16 at 13:50
  • 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! – razvan Sep 26 '16 at 14:10
  • I am facing the same issue. Xcode 8.0 Swift 2.3 HERE iOS SDK Premium Edition v3.2.1 – Burhanuddin Sunelwala Sep 26 '16 at 15:06

1 Answers1

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)
Burhanuddin Sunelwala
  • 5,318
  • 3
  • 25
  • 51
  • So the NMACoreRouter object gets released if we declare it within the function and hence the callback is not called. – Burhanuddin Sunelwala Sep 27 '16 at 06:25
  • @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! – Chaim Friedman Nov 23 '16 at 20:47
  • 1
    @ChaimFriedman the above code is the working example! Let us know what specifically you need? – Burhanuddin Sunelwala Nov 24 '16 at 05:49
  • 1
    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 weekend – Chaim Friedman Nov 25 '16 at 16:01
  • @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. Thanks – Chaim Friedman Feb 05 '17 at 06:04
  • 1
    navigationManager.startTurnByTurnNavigation(with: route) works absolutely fine. Are you implementing the delegate? Are you simulating the navigation for testing? – Burhanuddin Sunelwala Feb 05 '17 at 06:07
  • 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. – Chaim Friedman Feb 05 '17 at 13:44
  • ...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 @Marco 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. Thanks – Chaim Friedman Feb 05 '17 at 13:58
  • 1
    Thanks much. That helped. – Chaim Friedman Feb 05 '17 at 20:47