Getting Directions

Getting Directions to a Location

See an example of Directions here: BasicDirection.swift

In a lot of cases, the user might not want to display a specific region, but rather get a route proposed that will lead them to where they need to go. In order to accomplish this, we utilize the MPDirectionsService class, which we will be able to query through the MPDirectionsQuery class, while to actually display the result we use the MPDirectionsRenderer.

First things first, let us add in some new variables, namely the renderer and two points we wish to acquire directional data between (the origin and destination). Usually, the origin point would refer to the location of the user, however for the purposes of demonstration we will use a preset origin point (the Location found in Display a Map).

https://github.com/MapsPeople/MapsIndoorsGettingStarted-Mapbox/blob/12aaf3c15d8516842e76effddec95416f3a7e3c4/MapsIndoorsGettingStarted-Mapbox/ViewController.swift#L70-L74
    // Add the renderer property and origin point (populated when map shows, for demo purpose)
    lazy var directionsRenderer: MPDirectionsRenderer? = {
        mapControl?.newDirectionsRenderer()
    }()
    var origin: MPLocation?

In order to update the destination variable we simply employ the same concept as we did when adding the search bar, namely we utilise the func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) function. In this case, we simply need to update our function to store the point of the selected location and call directions(), which is a function we will add in a bit that will handle the construction of the directions query and rendering.

https://github.com/MapsPeople/MapsIndoorsGettingStarted-Mapbox/blob/12aaf3c15d8516842e76effddec95416f3a7e3c4/MapsIndoorsGettingStarted-Mapbox/ViewController%2BUITableViewDelegate.swift
import UIKit

extension ViewController: UITableViewDelegate {

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard let location = searchResult?[indexPath.row] else { return }
        mapControl?.goTo(entity: location)  // Use the retained mpMapControl object
        tableView.removeFromSuperview()

        // Call the directions(to:) function to get directions to the selected search result
        directions(to: location)
    }

}

We can now add in the directions() function:

https://github.com/MapsPeople/MapsIndoorsGettingStarted-Mapbox/blob/12aaf3c15d8516842e76effddec95416f3a7e3c4/MapsIndoorsGettingStarted-Mapbox/ViewController.swift#L54-L68
    // Get directions to the selected search result
    func directions(to destination: MPLocation) {
        let directionsQuery = MPDirectionsQuery(origin: origin!, destination: destination)

        Task {
            do {
                let route = try await MPMapsIndoors.shared.directionsService.routingWith(query: directionsQuery)
                directionsRenderer?.route = route
                directionsRenderer?.routeLegIndex = 0
                directionsRenderer?.animate(duration: 5)
            } catch {
                print("Error getting directions: \(error.localizedDescription)")
            }
        }
    }

Expected Result

Last updated

Was this helpful?