Mapbox Directions for Swift
Mapbox Directions for Swift makes it easy to connect your iOS, macOS, tvOS, or watchOS application to the Mapbox Directions and Map Matching APIs. Quickly get driving, cycling, or walking directions, whether the trip is nonstop or it has multiple stopping points, all using a simple interface reminiscent of MapKit’s MKDirections
API. Fit a GPX trace to the OpenStreetMap road network. The Mapbox Directions and Map Matching APIs are powered by the OSRM and Valhalla routing engines. For more information, see the Mapbox Navigation homepage.
Mapbox Directions pairs well with MapboxGeocoder.swift, MapboxStatic.swift, the Mapbox Navigation SDK for iOS, and the Mapbox Maps SDK for iOS or macOS SDK.
Installation
Specify the following dependency in your Carthage Cartfile:
github "mapbox/mapbox-directions-swift" ~> 1.2
Or in your CocoaPods Podfile:
pod 'MapboxDirections', '~> 1.2'
Or in your Swift Package Manager Package.swift:
.package(url: "https://github.com/mapbox/mapbox-directions-swift.git", from: "0.30.0")
Then import MapboxDirections
.
Configuration
You’ll need a Mapbox access token in order to use the API. If you’re already using the Mapbox Maps SDK for iOS or macOS SDK, Mapbox Directions automatically recognizes your access token, as long as you’ve placed it in the MGLMapboxAccessToken
key of your application’s Info.plist file.
Starting points
Directions
is the main class that represents the Mapbox Directions and Map Matching APIs. To calculate directions between coordinates, configure a RouteOptions
object and pass it into Directions.calculate(_:completionHandler:)
. Similarly, to match a trace to the road network, configure a MatchOptions
object and pass it into either Directions.calculate(_:completionHandler:)
or Directions.calculateRoutes(matching:completionHandler:)
. These methods asynchronously send requests to the API, then form Route
or Match
objects that correspond to the API’s response.
A Route
object is composed of one or more RouteLeg
s between waypoints, which in turn are composed of one or more RouteStep
s between maneuvers. Depending on the request, a RouteStep
may additionally contain objects representing intersection- and segment-level data. A Match
object is structured similarly, except that it provides additional details about how the trace matches the road network.
For further details, consult the guides and examples included with this API reference. To integrate real-time turn-by-turn navigation into your iOS application, see “Navigation SDK”. If you have any questions, please see our help page. We welcome your bug reports, feature requests, and contributions.
Changes in version 1.2.0
Packaging
- Added support for building and running on any Linux distribution supported by Swift. (#488)
- Added the
MapboxDirectionsCLI
command line tool that round-trips Mapbox Directions API responses between JSON format and Swift model objects. (#469)
Other changes
- Added the
RouteStep.segmentIndicesByIntersection
property for associatingIntersection
s with portions of the step’s shape. (#490) - Added the
Intersection.outletMapboxStreetsRoadClass
property that indicates a more detailed road classification than the existingIntersection.outletRoadClasses
property. (#507) - Added the
RouteLeg.incidents
property that indicates known traffic incidents, toll collection points, rest areas, and border crossings along the route leg. (#466, #506) - Added the
RouteLeg.regionCode(atStepIndex:intersectionIndex:)
method andIntersection.regionCode
property to get the administrative region where an intersection is located, as well as aRouteLeg.administrativeRegions
property that indicates the administrative regions traversed by the route leg. (#466, #485, #506) - Added the
Intersection.tunnelName
,Intersection.tollCollection
,Intersection.restStop
, andIntersection.isUrban
properties. (#466, #506) - The
CongestionLevel
enumeration now conforms to theCaseIterable
protocol. (#500)