问题描述
我正在尝试使用MapKit方向请求获取两个坐标之间的公交路线.
I'm trying to use the MapKit Directions Request to get transit directions between two coordinates.
当我切换到其他(非Transit)类型时,下面的代码有效,但是当我切换到Transit时,它将引发一个错误,该错误在Apple文档中均未显示.
When I switch to other (non-Transit) types, the code below works, but when I switch to Transit, it throws an error that doesn't show up anywhere in Apple's documentation.
这两个位置(来源和目的地)都在纽约市,因此肯定应该有公交路线可用.
The two locations (source and destination) are both in New York City so there should definitely be transit directions available.
错误消息:
Error Domain=MKErrorDomain Code=5 "(null)"
代码段:
override func viewDidLoad() {
super.viewDidLoad()
let request = MKDirectionsRequest()
// Set request parameters
request.source = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: 40.7127, longitude: -74.0059), addressDictionary: nil))
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: 40.6761, longitude: -73.9521), addressDictionary: nil))
request.requestsAlternateRoutes = true
// Set tranport type parameter (anything other than .Transit works)
request.transportType = .Transit
let directions = MKDirections(request: request)
directions.calculateDirectionsWithCompletionHandler { response, error in
print(response)
guard let routes = response?.routes else {
print(error?.description)
return
}
// Prints step-by-step directions
for r in routes {
print("New route")
for step in r.steps {
print(" " + step.instructions)
}
}
}
}
关于我在特定运输案例中可能做错了什么的任何建议?谢谢!
Any advice on what I might be doing wrong for the particular Transit case? Thank you!
推荐答案
当前不支持路线选择路线(iOS 9).如您所见,MKDirectionsRequest
将返回空错误.
Routing directions for transit is currently not supported (iOS 9). MKDirectionsRequest
will return a null error, as you observed.
这似乎仅直接记录在MapKit的标题中.看看Transit
类型的注释.
This only seems to be documented directly in MapKit's headers. Take a look at the comment for the Transit
type.
// MKDirectionsTypes.h
@available(iOS 7.0, *)
public struct MKDirectionsTransportType : OptionSetType {
public init(rawValue: UInt)
public static var Automobile: MKDirectionsTransportType { get }
public static var Walking: MKDirectionsTransportType { get }
@available(iOS 9.0, *)
public static var Transit: MKDirectionsTransportType { get } // Only supported for ETA calculations
public static var Any: MKDirectionsTransportType { get }
}
这篇关于传输MKDirectionsRequest产生空错误Error Domain = MKErrorDomain Code = 5“(null)".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!