Now I'm doing somethig nice with Swift 3, I hope: Calculating a route between 2 places. I am using the Xcode simulator. I can calculate it by car, walking, or with transit.
This is what I have:
mapview.delegate = self
let request = MKDirectionsRequest()
request.source = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: 40.203314, longitude: -8.410257), addressDictionary: nil))
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: 40.112808, longitude: -8.498689), addressDictionary: nil))
request.requestsAlternateRoutes = false
//request.transportType = .automobile
request.transportType = .transit
let directions = MKDirections(request: request)
directions.calculate { [unowned self] response, error in
guard let unwrappedResponse = response else { return }
for route in unwrappedResponse.routes {
self.mapview.add(route.polyline)
self.mapview.setVisibleMapRect(route.polyline.boundingMapRect, animated: true)
print("Distance: \(route.distance/1000) m")
print("ETA: \(route.expectedTravelTime/60) min")
for step in route.steps {
print(step.instructions)
}
}
}
I've set the code, and everything works for car or walking. However, transit options is not doing anything.
Why is that?
Directions are not available for transportType .transit unfortunately, only ETA
Related Post