Itinerary Service

The itineraries used in the WemapMapSDK are generated by the Wemap Routing API. When you install the WemapMapSDK, it also includes WemapCoreSDK which has ItineraryService class, which provides a convenient way to access the Wemap Routing API.

To generate an itinerary, you’ll need to create an instance of ItineraryService and request for itinerary computation using ItineraryParameters class. You also can use the ItinerarySearchOptions class to set a few options. Wemap Routing API provides different travel modes for itinerary computation: walk, car and bike. Bike travel mode also can have TravelMode.Preference: default, safest, fastest and tourism.

Below are examples of how you can use specific options to compute itineraries for a few scenarios.

The code examples below show how to use the WemapCoreSDK without the WemapMapSDK, but if you use WemapMapSDK - this logic is managed by ItineraryManager and NavigationManager of WemapMapSDK.

Compute itinerary

This example demonstrates how to calculate walking itineraries with origin and destination coordinates.

import RxCocoa
import RxSwift
import UIKit
import WemapCoreSDK

class ViewController: UIViewController {

    private let disposeBag = DisposeBag()
    private let service = ItineraryService()

    override func viewDidLoad() {
        super.viewDidLoad()
        computeItineraries()
    }

    private func computeItineraries() {

        let origin = Coordinate(latitude: 43.610628, longitude: 3.876654)
        let destination = Coordinate(latitude: 43.609011, longitude: 3.917091)

        let parameters = ItineraryParameters(origin: origin, destination: destination) // default travelMode is .walk

        service
            .itineraries(parameters: parameters)
            .subscribe(
                onSuccess: {
                    debugPrint("response - \($0)")
                }, onFailure: {
                    debugPrint("error - \($0)")
                }
            )
            .disposed(by: disposeBag)
    }
}

You can specify travel mode and preference for the request:

private func computeItineraries() {
    ...
    let parameters = ItineraryParameters(origin: origin, destination: destination, travelMode: .bike(preference: .safest))
    ...
}

Also is some cases you may need to specify search options:

private func computeItineraries() {
    ...
    let parameters = ItineraryParameters(origin: origin, destination: destination, travelMode: .walk, searchOptions: .init(avoidStairs: true))
    ...
}

The NavigationInfo class contains all information about the user’s progress along the itinerary, including the current leg and step information. This object allows you to get distance and time measurements, previous and next steps, and more.

The NavigationInfoHandler helps to get updated information about the user’s progress along the itinerary on request by providing an updated NavigationInfo object.

import RxCocoa
import RxSwift
import UIKit
import WemapCoreSDK

class ViewController: UIViewController {

    private let disposeBag = DisposeBag()
    private let service = ItineraryService()
    private let infoHandler = NavigationInfoHandler()
    private var itinerary: Itinerary?

    override func viewDidLoad() {
        super.viewDidLoad()
        computeItineraries()
    }

    private func computeItineraries() {

        let origin = Coordinate(latitude: 43.610628, longitude: 3.876654)
        let destination = Coordinate(latitude: 43.609011, longitude: 3.917091)

        let parameters = ItineraryParameters(origin: origin, destination: destination, travelMode: .bike(preference: .safest))

        service
            .itineraries(parameters: parameters)
            .subscribe(
                onSuccess: { [unowned self] response in
                    debugPrint("response - \(response)")
                    setItinerary(response.itineraries.first!)
                }, onFailure: {
                    debugPrint("error - \($0)")
                }
            )
            .disposed(by: disposeBag)
    }

    private func setItinerary(_ itinerary: Itinerary) {
        self.itinerary = itinerary
        infoHandler.itinerary = itinerary

        // request for an update of the navigation information with a certain time interval
        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(5)) { [unowned self] in
            getNavigationInfo(.init(latitude: 43.610107, longitude: 3.891805))
        }
    }

    private func getNavigationInfo(_ userPosition: Coordinate) {
        let info = infoHandler.getNavigationInfo(userPosition: userPosition)
        debugPrint("navigation info - \(String(describing: info))")
    }
}

Examples

You can find additional examples for the WemapSDKs on GitHub. Clone the repository and run the example application following the instructions in the README.