gpt4 book ai didi

ios - 使用 MKDirections 实例的计算方法

转载 作者:行者123 更新时间:2023-11-29 05:59:29 25 4
gpt4 key购买 nike

我在 for 循环中使用 MKDirections 对象的计算方法时遇到一个非常奇怪的问题。我的代码计算从用户当前位置到 3 个不同露营地位置的距离,但并非所有距离都是在代码中的同一点计算的。下面是我的代码;

        for siteIndex in 0...countySites.count - 1 {

var selectedSite = countySites[siteIndex]
let destinationPlacemark = MKPlacemark(coordinate: selectedSite.locationCoordinate)

request.destination = MKMapItem(placemark: destinationPlacemark)

let distanceAndDirections = MKDirections(request: request)

if currentCoordinate != nil {

distanceAndDirections.calculate { (response, error) in
if error == nil {

let distanceInMetres = response?.routes.first?.distance
let distanceInMiles = distanceInMetres! / 1610
let roundedDistanceInMiles = Int(distanceInMiles.rounded())
distanceToSite = roundedDistanceInMiles

selectedSite.distance = distanceToSite
self.countySites[siteIndex] = selectedSite

print("distanceToSite = \(distanceToSite)")

if self.countySites.count > 1 {
if siteIndex == self.countySites.count - 1 {

print("BEFORE: countySites[0].distance = \(self.countySites[0].distance)")
print("BEFORE: countySites[1].distance = \(self.countySites[1].distance)")
print("BEFORE: countySites[2].distance = \(self.countySites[2].distance)")

self.countySites = self.sortByDistance(sites: self.countySites)

print("AFTER: countySites[0].distance = \(self.countySites[0].distance)")
print("AFTER: countySites[1].distance = \(self.countySites[1].distance)")
print("AFTER: countySites[2].distance = \(self.countySites[2].distance)")

self.tableView.reloadData()
}
}

} else {

print("Distance error: \(error.debugDescription)")

}
}

} else {
print("currentCoordinate is nil")
}
}

当我在我的设备上运行代码时,我得到以下输出;

Got here!
distanceToSite = 224
distanceToSite = 235
BEFORE: countySites[0].distance = 224
BEFORE: countySites[1].distance = -1
BEFORE: countySites[2].distance = 235
AFTER: countySites[0].distance = -1
AFTER: countySites[1].distance = 224
AFTER: countySites[2].distance = 235
distanceToSite = 235

谁能解决这个问题吗?谢谢。

最佳答案

calculate(completionHandler:)以异步方式返回值,不能保证在到达列表中的最后一个元素后计算所有距离。要实现您的需要,您必须执行以下操作:

let dispatchGroup = DispatchGroup()

for siteIndex in 0 ..< countySites.count {
var selectedSite = countySites[siteIndex]

if let currentCoordinate = currentCoordinate {
let request = MKDirections.Request()
request.source = MKMapItem(placemark: MKPlacemark(coordinate: currentCoordinate))
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: selectedSite.locationCoordinate))
let distanceAndDirections = MKDirections(request: request)

dispatchGroup.enter()
distanceAndDirections.calculate { (response, error) in
if error == nil {

let distanceInMetres = response?.routes.first?.distance
let distanceInMiles = distanceInMetres! / 1610
let roundedDistanceInMiles = Int(distanceInMiles.rounded())
let distanceToSite = roundedDistanceInMiles

selectedSite.distance = distanceToSite
countySites[siteIndex] = selectedSite

print("distanceToSite = \(distanceToSite)")

} else {

print("Distance error: \(error.debugDescription)")

}
dispatchGroup.leave()
}

} else {
print("currentCoordinate is nil")
}
}

dispatchGroup.notify(queue: .main) {
print("BEFORE: countySites[0].distance = \(self.countySites[0].distance)")
print("BEFORE: countySites[1].distance = \(self.countySites[1].distance)")
print("BEFORE: countySites[2].distance = \(self.countySites[2].distance)")

self.countySites = self.sortByDistance(sites: self.countySites)

print("AFTER: countySites[0].distance = \(self.countySites[0].distance)")
print("AFTER: countySites[1].distance = \(self.countySites[1].distance)")
print("AFTER: countySites[2].distance = \(self.countySites[2].distance)")

self.tableView.reloadData()
}

您必须创建一个DispatchGroup并调用dispatchGroup.enter()在调用calculate(completionHandler:)之前。在同一方法的completionHandler中,您必须调用dispatchGroup.leave() 。一旦enterleave调用计数具有相同的值,dispatchGroup.notify 的闭包被执行。请注意,我替换了您的封闭范围 0...countySites.count - 1半开范围 0 ..< countySites.count .

亲切的问候

关于ios - 使用 MKDirections 实例的计算方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54829483/

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