---
title: "#48: MKDirections 🚩"
url: https://littlebitesofcocoa.com/48-mkdirections
author: Jake Marsh
published: 2015-07-29
topics: ["MapKit","Core Location"]
description: "MKDirections and MKDirectionsRequest are the classes we use to retrieve directions between two locations. They also make it very easy to display the..."
---

# #48: MKDirections 🚩

By Jake Marsh, July 29, 2015. HTML: https://littlebitesofcocoa.com/48-mkdirections

**`MKDirections`** and **`MKDirectionsRequest`** are the classes we use to retrieve directions between two locations. They also make it very easy to display the resulting directions route on a map. Let's dive in:

## Setup Directions Request

Note: Off camera, we have let the user input a keyword search **`String`**, then geocoded it into an **`MKPlacemark`** object called `placemark`.

``` swift
let request = MKDirectionsRequest()

request.source = MKMapItem.mapItemForCurrentLocation()

let destination = MKPlacemark(
  coordinate: placemark.location.coordinate,
  addressDictionary: nil
)

request.destination = MKMapItem(placemark: destination)
let d = MKDirections(request: request)

d.calculateDirectionsWithCompletionHandler { response, error in
  guard let route = response?.routes.first else { return }

  // we'll do this bit next
}
```

## Display Directions on Map

``` swift
self.directionsRoute = route

self.mapView?.addOverlay(route.polyline)
self.mapView?.setVisibleMapRect(
  route.polyline.boundingMapRect,
  animated: true
)
```

Finally, we implement this **`MKMapViewDelegate`** method:

``` swift
func mapView(mapView: MKMapView,
rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
  let renderer = MKPolylineRenderer(
    polyline: directionsRoute.polyline
  )

  renderer.lineWidth = 2
  renderer.strokeColor = UIColor.blueColor()

  return renderer
}
```

<img src="https://media.littlebitesofcocoa.com/bite-assets/048-mkdirections-map-view.png" style="max-width: 130px;" />
