现在我正在用 Swift 3 做一些不错的事情,我希望:计算 2 个地方之间的路线。 我正在使用 Xcode 模拟器。 我可以通过开车、步行或乘坐公交车来计算。
这是我的:
mapview.delegate = self
let request = MKDirectionsRequest()
request.source = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: 40.203314, longitude: -8.410257), addressDictionary: nil))
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: 40.112808, longitude: -8.498689), addressDictionary: nil))
request.requestsAlternateRoutes = false
//request.transportType = .automobile
request.transportType = .transit
let directions = MKDirections(request: request)
directions.calculate { [unowned self] response, error in
guard let unwrappedResponse = response else { return }
for route in unwrappedResponse.routes {
self.mapview.add(route.polyline)
self.mapview.setVisibleMapRect(route.polyline.boundingMapRect, animated: true)
print("Distance: \(route.distance/1000) m")
print("ETA: \(route.expectedTravelTime/60) min")
for step in route.steps {
print(step.instructions)
}
}
}
我已经设置了代码,一切都适用于汽车或步行。但是,transit options 没有做任何事情。
这是为什么?
最佳答案
方向不适用于 transportType .transit 不幸的是,只有 ETA
关于swift - MapKit - MKDirectionsRequest() 的传输类型不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40951882/