[CLLocation]
我有一个以 a作为输入的函数。在while
循环内部,它将它分成块,对于每个块发出MKDirection
请求,将响应存储在一个新的中[CLLocation]
,并在完成后返回它。
问题是新数组中的所有块都不是连续的,因此生成的路由会到处跳转。如何在创建新请求之前等待上一个请求得到响应?我试过了DispatchQueue.global().sync
,DispatchQueue.main.sync
但这并没有什么不同。我试图从无法等待 MKDirections.calculate 的结果中实现第一个答案,得到 nil 而不是它,这似乎是我同样的问题,但我不明白如何使其适应我的情况。你能帮我按顺序得到回复吗?这是函数,注释掉的部分是路由的最新位,这将是最后一个请求。一如既往地感谢您的帮助和时间。
func repositionLocation2(route: [CLLocation], completion: @escaping ([CLLocation]) -> Void) {
let group = DispatchGroup()
var pos = 0
var nextPos = 3
var repositioned = [CLLocation]()
// repositioned.append(route.first!)
guard route.count > nextPos else {print("Reposision Location failed, not enough positions");return}
let request = MKDirections.Request()
request.requestsAlternateRoutes = false
request.transportType = .walking
while pos < route.count - nextPos {
print(" pos in \(pos)")
// repositioned.removeAll()
group.enter()
// get a small chunk of the input route
let a = route[pos].coordinate//repositioned.last!.coordinate//
let b = route[pos + nextPos].coordinate
// get directions for the small chunk
request.source = MKMapItem(placemark: MKPlacemark(coordinate: a))
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: b))
let directions = MKDirections(request: request)
// DispatchQueue.main.sync {
// DispatchQueue.global().sync {
// group.enter()
directions.calculate { [unowned self] response, error in
if let err = error {
print("direction error : \(err)")
}
guard let unwrappedResponse = response else {print("no suggested routes available"); return }
print("Response is: \(unwrappedResponse.debugDescription)")
guard let coord = unwrappedResponse.routes.first?.steps else {print("No coordinates");return}
print("coord is: \(coord)")
// save response coordinates into a new array
for location in coord {
let point: CLLocation = CLLocation(latitude: location.polyline.coordinate.latitude, longitude: location.polyline.coordinate.longitude)
print("point is: \(point)") // prints a correct CLLocation with coordinates
repositioned.append(point)
print("repositioned in for loop is : \(repositioned)") // prints just first appended location CLLocation with coordinates
// group.leave()
}
// group.wait() // hangs the app
completion(repositioned)
}
// }
print("repositioned in while loop is : \(repositioned)")
// shift to nex addiacent chunk
pos += 3
nextPos += 3
}
// // last chunk
// let a = route[pos - 5].coordinate//repositioned.last!.coordinate
// let b = route.last?.coordinate
// request.source = MKMapItem(placemark: MKPlacemark(coordinate: a))
// request.destination = MKMapItem(placemark: MKPlacemark(coordinate: b!))
// let directions = MKDirections(request: request)
// directions.calculate { [unowned self] response, error in
// if let err = error {
// print("direction error : \(err)")
// }
// guard let unwrappedResponse = response else {print("no suggested routes available"); return }
// print("Response is: \(unwrappedResponse.debugDescription)")
// guard let coord = unwrappedResponse.routes.first?.steps else {print("No coordinates");return}
// print("coord is: \(coord)")
// for location in coord {
//
// let point: CLLocation = CLLocation(latitude: location.polyline.coordinate.latitude, longitude: location.polyline.coordinate.longitude)
// print("point is: \(point)")
// repositioned.append(point)
// print("repositioned in for loop is : \(repositioned)")
// }
// completion(repositioned)
// }
// print("repositioned in while loop is : \(repositioned)")
}