作者:暗影HK4164286 | 来源:互联网 | 2023-05-22 20:10
如果我想获得公路距离,我应该考虑哪些课程?
我想要的只是从A点到B点的道路距离,我不需要显示一步一步的指示,也不需要显示地图.
有什么建议?
谢谢
1> Onato..:
您需要发出MKDirections请求.从calculateDirectionsWithCompletionHandler您将获得MKDirectionsResponse.这有一个MKRoutes的路由数组.每条路线都有一个距离(即道路距离)属性.
let source = MKMapItem( placemark: MKPlacemark(
coordinate: CLLocationCoordinate2DMake(-41.27, 173.28),
addressDictionary: nil))
let destination = MKMapItem(placemark: MKPlacemark(
coordinate: CLLocationCoordinate2DMake(-41.11, 173),
addressDictionary: nil))
let directiOnsRequest= MKDirectionsRequest()
directionsRequest.source = source
directionsRequest.destination = destination
let directiOns= MKDirections(request: directionsRequest)
directions.calculateDirectionsWithCompletionHandler { (response, error) -> Void in
print(error)
let distance = response!.routes.first?.distance // meters
print("\(distance! / 1000)km")
}
我每次使用此代码时都会得到这个错误Domain = MKErrorDomain Code = 3"Directions Not Available"UserInfo = 0x7fc96da18bf0 {NSLocalizedFailureReason =此时路由信息不可用.,MKErrorGEOError = -4,MKDirectiOnsErrorCode= 2,NSLocalizedDescription = Directions Not可用}致命错误:在解包可选值时意外发现nil