swift2 无法在Swift 2.0中设置MKDirectionsRequest的源和目标

ngynwnxp  于 2022-11-06  发布在  Swift
关注(0)|答案(1)|浏览(207)

当我尝试在Swift 2.0中使用MKDrectionsRequest时,我收到错误消息:
'型别'MKDirectionsRequest'的值没有成员'setSource'
我的程式码如下所示:

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

FYI:我甚至不需要实际的方向,只是估计的驾驶时间和距离,所以如果有其他的方法,请让我知道。谢谢。

qzlgjiam

qzlgjiam1#

您需要将这些值赋给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)
    }
}

相关问题