Skip to content

MKDirections.Request Doesn't Support Public Transit Directions

ETA calculations only!

Stuart Breckenridge
Stuart Breckenridge
1 min read
MKDirections.Request Doesn't Support Public Transit Directions
Photo by Javier Allegue Barros / Unsplash

I thought about adding some level of routing functionality to Singapore Buses as Maps does support public transit directions in Singapore.

After writing the code to request transit directions between two bus stops, I kept getting a nil response:

let stop1 = getBusStop(code: "61021")
let stop2 = getBusStop(code: "11031")

let request = MKDirections.Request()
request.source = MKMapItem(placemark: MKPlacemark(coordinate: stop1.coordinate))
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: stop2.coordinate))
request.requestsAlternateRoutes = false
request.transportType = .transit

let directions = MKDirections(request: request)

directions.calculate { [unowned self] response, error in
    guard let unwrappedResponse = response else { return } // <- returns here

    for route in unwrappedResponse.routes {
        self.busStopMap.addOverlay(route.polyline)
                self.busStopMap.setVisibleMapRect(route.polyline.boundingMapRect, animated: true)
        }
    }
}

Changing request.transportType to .automobile generates results, but .transit always returned nil. After a bit of digging in the MKDirectionsTransportType headers, I came across this:

@available(iOS 9.0, *)
public static var transit: MKDirectionsTransportType { get } // Only supported for ETA calculations

Damn!

📱App Development