Skip to content

Routing

This section explains the use of route computing functionality including multiple waypoints and alternate routes within an iOS app.

Car and Pedestrian Routing

The Sygic Maps SDK for iOS 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

The following example shows how to calculate a route from startpoint to endpoint. It's based on example from Getting Started, with added createRoute() and SYRoutingDelegate.

import UIKit
import SygicMaps

class ViewController: UIViewController {

    var mapView: SYMapView!
    var mapObjects = [SYMapObject]()
    lazy var routing = SYRouting()

    override func viewDidLoad() {
        super.viewDidLoad()

        if SYContext.isInitialized() {
            setupMapView()
        } else {
            let center = NotificationCenter.default
            var token: NSObjectProtocol?
            token = center.addObserver(forName: .DidInitSygicMaps, object: nil, queue: nil) { [weak self] (_) in

                center.removeObserver(token!)
                self?.setupMapView()
                self?.createRoute()
            }
        }
    }

    func setupMapView() {
        mapView = SYMapView(frame: view.bounds)
        mapView.camera.zoom = 17
        mapView.camera.tilt = 0
        mapView.camera.geoCenter = SYGeoCoordinate(latitude: 48.145810, longitude: 17.126420)

        mapView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(mapView)

        NSLayoutConstraint.activate([
            mapView.topAnchor.constraint(equalTo: view.topAnchor),
            view.bottomAnchor.constraint(equalTo: mapView.bottomAnchor),
            mapView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            view.trailingAnchor.constraint(equalTo: mapView.trailingAnchor)
        ])
    }

    func createRoute() {
        routing.delegate = self

        let start = SYWaypoint(
            position: SYGeoCoordinate(latitude: 48.158080, longitude: 17.127770),
            type: .start,
            name: nil
        )
        let end = SYWaypoint(
            position: SYGeoCoordinate(latitude: 48.373560, longitude: 17.598250),
            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

        let request = SYRouteRequest(start: start, destination: end, options: routingOptions, viaPoints: nil)
        routing.computeRoute(for: request)
    }
}

extension ViewController: SYRoutingDelegate {
    func routing(_ routing: SYRouting, didComputePrimaryRoute route: SYRoute?) {
        guard let route = route else { return }

        // remove old routes and labels from the map
        mapView.remove(mapObjects)

        // add new route to the map
        mapObjects = [
            SYMapRoute(route: route, type: .primary),
            SYMapRouteLabel(text: "My route", textStyle: nil, placeOn: route)
        ]
        mapView.add(mapObjects)

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

iOS Routing

SYWaypoint

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

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

// Pass it to SYRouteRequest
let request = SYRouteRequest(start: start, destination: end, options: routingOptions, viaPoints: [waypoint])

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()
if let jsonSerializedRoute = serializer.serializeRoute(route) {

    // Computing route from JSON
    routing.computeRoute(fromJson: jsonSerializedRoute)
}