Car and Pedestrian Routing

The Sygic iOS SDK supports route calculation with multiple waypoints, optimized for walking or driving. A route describes a path between at least two waypoints, the starting point and the destination, with optional intermediate waypoints in between.

Applications can provide route information to users in two ways:

  • A line rendered on a map that displays a connecting path between all waypoints
  • Turn-by-turn directions in text format

SYRoutingOptions
Represents the routing options (represented by the SYRoutingOption enums) that are applicable for the selected route

SYTransportmode property of SYRoutingOptions class provides following modes of routing:

  • Car
  • Pedestrian
  • Unknown

Following example shows how to calculate the route from startpoint to endpoint and start the navigation.

class RoutingViewController: UIViewController, SYRoutingDelegate {

    var mapView: SYMapView?
    let routing = SYRouting()

    func initMap() {
        self.mapView = SYMapView(frame: self.view.bounds)
        if let _mapView = self.mapView {
            _mapView.delegate = self
            _mapView.geoCenter = SYGeoCoordinate(latitude: 48.145810, longitude: 17.126420)!
            _mapView.zoom = 17
            self.view.addSubview(_mapView)

            // Setup tapGesture recognizer
            let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.tapped(_:)))
            _mapView.addGestureRecognizer(tapGestureRecognizer)

            self.computeRoute(
                from: SYGeoCoordinate(latitude: 48.158080, longitude: 17.127770)!,
                to: SYGeoCoordinate(latitude: 48.373560, longitude: 17.598250)!
            )
        }
    }

    func computeRoute(from fromCoordinate: SYGeoCoordinate, to toCoordinate: SYGeoCoordinate) {
        // Make self a delegate for SYRouting to receive and handle SYRoutingDelegate responses
        self.routing.delegate = self

        // Create SYWaypoint from coordinate. Set correct SYWaypointType for start and finish.
        let startWaypoint = SYWaypoint(position: fromCoordinate, type: .start, name: nil)
        let endWaypoint = SYWaypoint(position: toCoordinate, type: .end, name: nil)

        // Optionally: create an instance of SYRoutingOptions for configuring computing of a route
        let routingOptions = SYRoutingOptions()
        routingOptions.transportMode = .car // For other options see SYTransportMode
        routingOptions.routingType = .fastest // For other options see SYRoutingType

        // Start computing route
        self.routing.computeRoute(startWaypoint, to: endWaypoint, via: nil, with: routingOptions)
    }

    // MARK: - SYRoutingDelegate

    func routing(_ routing: SYRouting, didComputePrimaryRoute route: SYRoute?) {
        if let _mapView = self.mapView, let _route = route {
            // remove previously displayed map objects
            _mapView.remove(_mapView.allMapObjects())

            // Add Route object to map with label
            let mapRoute = SYMapRoute(route: _route, type: SYMapRouteType.primary)
            _mapView.add(mapRoute)
            _mapView.add(
                SYMapRouteLabel(text: "Main route", textStyle: nil, placeOn: _route)
            )

            // zoom out with animation to display full route
            _mapView.setViewBoundingBox(
                _route.box,
                with: UIEdgeInsets.init(top: 0.15, left: 0.15, bottom: 0.15, right: 0.15),
                duration: 1.0,
                curve: SYMapAnimationCurve.accelerateDecelerate,
                completion: nil
            )
        }
    }
}

SYWaypoint

You can use SYWaypoint to add waypoint(s) details to a car route calculation.

// Create SYWaypoint with type .via
let waypoint = SYWaypoint(position: fromCoordinate, type: .via, name: nil)

// Pass it to SYRouting. Other parts remains.
self.routing.computeRoute(startWaypoint, to: endWaypoint, via: [waypoint], with: routingOptions)

Route Serialization

Route serialization is a feature that allows users to use the new SYRoute method serializeRoute: to serialize a route into NSData, which can then be saved as a file. This is useful when a user would like to recover from a crash during navigation or when a user would like to transfer a route from one device to another.

The following is an example of how to invoke the serialization APIs:

// Serialization
let serializer = SYRouteSerializerBrief()
let jsonSerializedRoute = serializer.serializeRoute(route)

// Computing route from json
self.routing.computeRoute(fromJson: jsonSerializedRoute)