尝试在Swift 2.0中使用MKDrectionsRequest时,出现错误:


  '类型'MKDirectionsRequest'的值没有成员'setSource'


我的代码如下所示:

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


仅供参考:我什至不需要实际的方向,只需要估计的行驶时间和距离,所以如果还有其他方法可以获取,请告诉我。谢谢。

最佳答案

您需要将这些值分配给MKDirectionsRequestsource属性:

myRouteRequest.source = MKMapItem.mapItemForCurrentLocation()


destination同样适用:

myRouteRequest.destination = MKMapItem(placemark: myPlacemark)


此外,您在这里有错别字:

myrouteRequest.transportType = .Automobile


应为:

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


至于获得估计的旅行时间和距离:

我们首先需要创建路线请求:

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)
    }
}

关于ios - 无法在Swift 2.0中设置MKDirectionsRequest的源和目标,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33511257/

10-11 14:49