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 setup a query for a route and display the result on the map:

let directions = MPDirectionsService.init()
let renderer = MPDirectionsRenderer.init()

let origin = MPPoint.init(lat: 57.057917, lon: 9.950361, zValue:0)
let destination = MPPoint.init(lat: 57.058038, lon: 9.950509, zValue:0)

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

directions.routing(with: directionsQuery) { (route, error) in = self.googleMap
renderer.route = route
renderer.routeLegIndex = 0

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