Michael Kaminowitz
Michael Kaminowitz

Reputation: 335

Cannot set Source & Destination for MKDirectionsRequest in Swift 2.0

When trying to use MKDrectionsRequest in Swift 2.0, I get the error:

'Value of type 'MKDirectionsRequest' as no member 'setSource'

My code looks like this:

let myRouteRequest = MKDirectionsRequest()
myrouteRequest.transportType = .Automobile
myRouteRequest.setSource(MKMapItem.mapItemForCurrentLocation())
myRouteRequest.setDestination(MKMapItem(myPlacemark))

FYI: I don't even need the actual directions, just the estimated driving time and distance so if there is another way to get that please let me know. Thank you.

Upvotes: 0

Views: 1489

Answers (1)

Unheilig
Unheilig

Reputation: 16302

You'd need to assign those values to MKDirectionsRequest's source property:

myRouteRequest.source = MKMapItem.mapItemForCurrentLocation()

The same applies for destination:

myRouteRequest.destination = MKMapItem(placemark: myPlacemark)

In addition, you have a typo here:

myrouteRequest.transportType = .Automobile

as it should be:

myRouteRequest.transportType = .Automobile 
//Capital "R" is probably what you wanted to mean.

As for getting the estimated travel time and distance:

We'd need to first create a directions' request:

let directions = MKDirections(request: myRouteRequest)

directions.calculateDirectionsWithCompletionHandler
{
    (response, error) -> Void in

    if let routes = response?.routes where response?.routes.count > 0 && error == nil
    {
        let route : MKRoute = routes[0]

        //distance calculated from the request
        print(route.distance) 

        //travel time calculated from the request
        print(route.expectedTravelTime)
    }
}

Upvotes: 3

Related Questions