Traffic

public protocol Traffic : AnyObject

Interface to the Traffic Data Provider.

Navigation SDK components use the Traffic interface to access traffic information. For example, TomTomSDKRoutePlannerOffline.OfflineRoutePlanner uses it to calculate routes that avoid traffic.

An instance of this interface is created with TomTomSDKTrafficEngine.TrafficFactory.

Once a traffic data provider instance is created and connected to a source of traffic, it will start receiving traffic data as offered by the provider. For example, near the current location, along the route, or for the current country/area. If a route is registered via the Traffic.updateRoutesForTraffic function, the traffic data provider will start receiving traffic information for the given route. When new traffic events are received, the traffic data provider will notify the user via the TrafficUpdateObserver interface, provided that an observer is registered via the Traffic.addObserver function. Users can fetch traffic in an area via the Traffic.trafficEvents and Traffic.getTrafficEvents functions.

Important

This is a Public Preview API. It may be changed or removed at any time.
  • Indicates the traffic provider connectivity status.

    Important

    This is a Public Preview API. It may be changed or removed at any time.

    true if connection has been established, false otherwise.

    Declaration

    Swift

    var isConnected: Bool { get }
  • Number of traffic events currently known by the client.

    Important

    This is a Public Preview API. It may be changed or removed at any time.

    Declaration

    Swift

    var trafficEventCount: Int { get }
  • Updates the traffic client’s Locale.

    Important

    This is a Public Preview API. It may be changed or removed at any time.

    Note

    Locale is used to select the desired locale when receiving multi-lingual data or when extracting textual location details from the map.

    Declaration

    Swift

    func updateLocale(_ locale: Locale)

    Parameters

    locale

    The new Locale.

  • Sets a new location provider.

    Important

    This is a Public Preview API. It may be changed or removed at any time.

    Declaration

    Swift

    func updateLocationProvider(_ locationProvider: LocationProvider)

    Parameters

    locationProvider

    The new LocationProvider.

  • Updates the list of routes for which the client is interested in receiving traffic along the route.

    Important

    This is a Public Preview API. It may be changed or removed at any time.

    Providing an empty list disables traffic along the route. It can be enabled again by providing new routes via this method.

    Note that the availability of traffic along the route depends on the currently active traffic provider.

    Receiving traffic along a navigated route can be useful to display traffic information for the route. However, it can also have a negative impact on planning routes onboard or finding faster alternatives.

    Throws

    An error if routes cannot be updated.

    Declaration

    Swift

    func updateRoutesForTraffic(_ routes: [[CLLocationCoordinate2D]]) throws

    Parameters

    routes

    The list of route poly-lines for which to receive and update traffic information.

  • trafficEvents(for:) Asynchronous

    Gets traffic events within a boundig box .

    Important

    This is a Public Preview API. It may be changed or removed at any time.

    Throws

    An error if cannot get TrafficEvents.

    Declaration

    Swift

    func trafficEvents(for boundingBox: BoundingBox) async throws -> [TrafficEvent]

    Parameters

    boundingBox

    The area to look for the TrafficEvents.

    Return Value

    Array of TrafficEvents within the given boundingBox.

  • Asynchronously calls a completion callback with a result containing TrafficEvents within the given boundingBox, or an error.

    Important

    This is a Public Preview API. It may be changed or removed at any time.

    Declaration

    Swift

    func getTrafficEvents(
        for boundingBox: BoundingBox,
        completion: @escaping (Result<[TrafficEvent], Error>) -> ()
    )

    Parameters

    boundingBox

    The area to look for the TrafficEvents.

    completion

    The completion handler to call with the result. The handler may be executed on a background thread.

  • Adds an observer.

    Important

    This is a Public Preview API. It may be changed or removed at any time.

    Important

    An attempt to add an observer from the onTrafficUpdated() implementation of a registered observer may result in a deadlock.

    Declaration

    Swift

    func addObserver(_ observer: TrafficUpdateObserver)

    Parameters

    observer

    An instance of TrafficUpdateObserver to add.

  • Removes an observer.

    Important

    This is a Public Preview API. It may be changed or removed at any time.

    Important

    An attempt to remove an observer from the onTrafficUpdated() implementation of a registered observer may result in a deadlock.

    Declaration

    Swift

    func removeObserver(_ observer: TrafficUpdateObserver)

    Parameters

    observer

    An instance of TrafficUpdateObserver to remove.