A typical scenario could be to calculate travel time and directions for any of the selected search results. Based on a selectedResult we can calculate a route of type MKRoute within a getDirections() function.
Configure a MKDirections.Request() and with source location and destination location. In this example, we are using a fixed location as the source location, but of course, this could be the user's current location or any other coordinate.
func getDirections() {
self.route = nil
// Check if there is a selected result
guard let selectedResult else { return }
// Coordinate to use as a starting point for the example
let startingPoint = CLLocationCoordinate2D(
latitude: 40.83657722488077,
longitude: 14.306896671048852
)
// Create and configure the request
let request = MKDirections.Request()
request.source = MKMapItem(placemark: MKPlacemark(coordinate: startingPoint))
request.destination = self.selectedResult
// Get the directions based on the request
Task {
let directions = MKDirections(request: request)
let response = try? await directions.calculate()
route = response?.routes.first
}
}
The request is run asynchronously. Once you get a response you can access the valid route objects and then add them to a property in your view, so you can use them in your interface. Here we have a property called route that will store the first valid route from our response object.
Let’s use the .onChange modifier to monitor the selectedResult property and whenever it changes, we call the getDirections() function.
Lastly, in our Map view, we can add a MapPolyLine object if a route is available. We will use a blue stroke to display the way to the selected point of interest.
You can also get other types of information from a route, like the travel time between the source and destination locations. Let’s create a computed property called travelTime. It uses the route object to read and return its expected travel time through the expectedTravelTime property. To get it in the format of hours and minutes use a formatter object.
Here is a completely functional example showing the route between two hard-coded points on a Map view: