Getting directions

Last updated:

Now we have a simple map with a floor selector where can search for locations. When finishing this step you'll be able to create a directions between two points and change the transportation mode.

Get directions between two locations

Use the MPDirectionsService class to search for directions. You need to build a query using the MPDirectionsQuery class.

This example shows how to modify the view controller with a query for a route and display the result on the map:

class MapViewController: UIViewController {
private var mapControl: MPMapControl?
private let renderer = MPDirectionsRenderer()

override func viewDidLoad() {
super.viewDidLoad()
mapControl = MPMapControl(map: mapView)
directions()
}

func directions() {
let directions = MPDirectionsService()

let origin = MPPoint(lat: 38.897382, lon: -77.037447, zValue:0)
let destination = MPPoint(lat: 38.897583, lon: -77.037821, zValue:0)

let directionsQuery = MPDirectionsQuery(originPoint: origin!, destination: destination!)

directions.routing(with: directionsQuery) { (route, error) in
self.renderer.map = self.mapView
self.renderer.route = route
self.renderer.routeLegIndex = 0
self.renderer.animate(5)
}
}
}

The route object is seperated into objects of MPRouteLeg and these legs are again seperated indo objects of MPRouteStep. A specific part of the route can be rendered by setting the routeLegIndex and/or routeStepIndex properties.

renderer.routeLegIndex = 1

The length of the legs and steps arrays determines the possible values of routeLegIndex and routeStepIndex (0 ..< length).

Change transportation mode

In MapsIndoors, the transportation mode is referred to as travel mode. There are four travel modes, walking, bicycling, driving and transit (public transportation). The travel modes generally applies for outdoor navigation. Indoor navigation calculations are based on walking travel mode.

Set travel mode on your request using the travelMode property on MPDirectionsQuery:

let directionsQuery = MPDirectionsQuery.init(originPoint: origin!, destination: destination!)
directionsQuery.travelMode = .driving

Next up: Live Data