gpt4 book ai didi

ios - 无法在 Swift 2.0 中为 MKDirectionsRequest 设置源和目标

转载 作者:行者123 更新时间:2023-11-28 19:43:09 24 4
gpt4 key购买 nike

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

'Value of type 'MKDirectionsRequest' as no member '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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com