I currently have two fields/properties within my view controller. We are using the calculateDirectionsWithCompletionHandler
and trying to set my fields to the value of route.distance
and route.expectedTravelTime
. Here is the code for that:
func calculateDistanceAndEta(locationCoordinate: CLLocationCoordinate2D) {
let currentLocMapItem = MKMapItem.mapItemForCurrentLocation();
let selectedPlacemark = MKPlacemark(coordinate: locationCoordinate, addressDictionary: nil);
let selectedMapItem = MKMapItem(placemark: selectedPlacemark);
let mapItems = [currentLocMapItem, selectedMapItem];
let request: MKDirectionsRequest = MKDirectionsRequest()
request.transportType = MKDirectionsTransportType.Walking;
request.setSource(currentLocMapItem)
request.setDestination(selectedMapItem);
var directions: MKDirections = MKDirections(request: request);
var distsanceLabelTest = ""
var etaLabelTest = ""
directions.calculateDirectionsWithCompletionHandler { (response, error) -> Void in
if (error == nil) {
if (response.routes.count > 0) {
var route: MKRoute = response.routes[0] as! MKRoute;
// route.distance = distance
// route.expectedTravelTime = eta
println("\(route.distance)")
distsanceLabelTest = "\(route.distance)"
etaLabelTest = "\(route.expectedTravelTime)"
}
} else {
println(error)
}
}
println(distsanceLabelTest)
println(etaLabelTest)
self.distanceLabelString = distsanceLabelTest
self.etaLabelString = etaLabelTest
}
However, we can't seem to set any of the variables as it just returns nil
. How do we set our class fields to the values of route.distance
and route.expectedTravelTime
.