gpt4 book ai didi

ios - 我可以从mapView上已有的方向获取大概的旅行时间吗?

转载 作者:行者123 更新时间:2023-12-01 19:39:55 25 4
gpt4 key购买 nike

我正在尝试显示从用户当前位置到某个位置的大概行进时间,并且方向已经出现在 map 上。我想知道这是否可行,因为当我搜索此内容时似乎什么都没有出现。如果可能的话,迅速会比Objective-C更好。

最佳答案

您已经在 map 上显示了路线,并且您的要求是使用Apple map 。我认为您正在使用MKDirectionsRequest来获取和显示路线。使用MKDirectionsRequest,您可以找到路线和可能的路线。您可以指定所需的方向类型(汽车,公交,步行),并从route中获取预计的旅行时间。为了您的方便,我添加了完整的代码。

        let request = MKDirectionsRequest()
request.source = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: startLocation?.latitude, longitude: startLocation?.longitude), addressDictionary: nil))
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: endLocation?.latitude, longitude: endLocation?.longitude), addressDictionary: nil))
request.requestsAlternateRoutes = true // if you want multiple possible routes
request.transportType = .automobile // will be good for cars

现在获取指示
        let directions = MKDirections(request: request)
directions.calculate {(response, error) -> Void in

guard let response = response else {
if let error = error {
print("Error: \(error)")
}
return
}

// Lets Get the first suggested route and its travel time

if response.routes.count > 0 {
let route = response.routes[0]
print(route.expectedTravelTime) // it will be in seconds
// you can show this time in any of your UILabel or whatever you want.
}
}

关于ios - 我可以从mapView上已有的方向获取大概的旅行时间吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55584316/

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