Sygic issue in calculating the route

239 Views Asked by At

I have a problem when I tried to add a route in my Sygic offline map view. In the debugger, I got this message " Routing interface: Asking for unknown transport mode." this the code that I wrote:

   class SygicMapViewController: UIViewController, SYNavigationDelegate,SYRoutingDelegate,SYMapViewDelegate,CLLocationManagerDelegate{
    let mapView = SYMapView()
    let routing = SYRouting()
    var  locationManager = CLLocationManager()
    override func viewDidLoad() {
        super.viewDidLoad()
        mapView.delegate = self
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
        locationManager.requestAlwaysAuthorization()
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
    }
    func setupMapView(){
        let tiltFor2D: SYAngle = 0
        mapView.tilt = tiltFor2D
        mapView.zoom = 12
        mapView.rotation = 180
        mapView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height:self.view.frame.size.height+20)
        self.view.addSubview(mapView)
        mapView.renderEnabled = true
    }
    func addMarkers(){

    }
    func mapView(_ mapView: SYMapView, didFinishInitialization success: Bool) {
        setupMapView()
        SYNavigation.shared().delegate = self
        routing.delegate = self
        let startPoint = SYGeoCoordinate(latitude: 37.270665, longitude:  9.873921)
        let endPoint = SYGeoCoordinate(latitude: 37.242681, longitude: 9.911932)
        computeRoute(from: startPoint!, to: endPoint!)
    }
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func computeRoute(from fromCoordinate: SYGeoCoordinate, to toCoordinate:
        SYGeoCoordinate) {
        let startWaypoint = SYWaypoint(position: fromCoordinate, type: .start, name: "Begin")
        let endWaypoint = SYWaypoint(position: toCoordinate, type: .end, name: "End")
        let routingOptions = SYRoutingOptions()
        routingOptions.transportMode = .unknown// For other options see SYTransportMode
        routingOptions.routingType = .economic// For other options see SYRoutingType
        routing.computeRoute(startWaypoint, to: endWaypoint, via: nil, with: routingOptions)
    }

    func routing(_ routing: SYRouting, computingFailedWithError error: SYRoutingError) {
        print(error)
    }

    func routing(_ routing: SYRouting, didComputePrimaryRoute route: SYRoute?) {
        SYNavigation.shared().start(with: route)
        // You might want to put it also on the map
        let mapRoute = SYMapRoute(route: route!, type: .primary)
        mapView.add(mapRoute)
        mapView.cameraMovementMode = .followGpsPosition
    }
}

I correct the issue by selecting the local routing variable in the computeRoute method and I made some changes to fire the computeRoute method after the mapview Intialization

1

There are 1 best solutions below

3
On

I actually see two issues with your code.

  1. In func computeRoute(from fromCoordinate: SYGeoCoordinate, to toCoordinate: SYGeoCoordinate) you define local variable routing which hides instance variable. This causes the routing to be deallocated before it can compute any route. So just remove that let routing = SYRouting() from that method and use instance routing variable.
  2. The other one can also be an issue and cause problems. You start routing in viewDidLoad before map is initialised. Then if the routing is faster you will be adding SYRoute object to map and that would probably crash. I know this is just some quick setup, but to avoid any issues move that call to computeRoute at least to mapView(_, didFinishInitialization) method.