gpt4 book ai didi

ios - 计算从当前位置到 x 个对象的距离,并使用从最近到最远的过滤器对它们进行排序

转载 作者:行者123 更新时间:2023-11-30 11:57:32 26 4
gpt4 key购买 nike

我创建了两个函数来计算从我的位置到 x 汽车的距离

func distance(from: CLLocationCoordinate2D, to: CLLocationCoordinate2D) -> CLLocationDistance {
let from = CLLocation(latitude: from.latitude, longitude: from.longitude)
let to = CLLocation(latitude: to.latitude, longitude: to.longitude)

return from.distance(from: to)
}


func selT(car:Car) {
guard let coordinates = car.location else {
return
}
self.destination = coordinates

if currentLocation != nil {
let dist = distance(from: currentLocation!, to: coordinates)
}
}

我有一个下载汽车的功能

func getOkToDownload(download:CarsToDownload?, error : Error?) -> Void {       
if let download = download {
self.download = download
if download.status == "OK" {
if let carsDownloaded = download.cars {
var number = numberCars / (categories?.count)!
number = number == 0 ? 1 : number

let distanceC = carsDownloaded

cars.append(contentsOf: carsDownloaded)
}

self.tableView?.reloadData()
} else {
let alert = UIAlertController.init(title: "Error", message: response.status, preferredStyle: .alert)
alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))
alert.addAction(UIAlertAction.init(title: "Retry", style: .default, handler: { (action) in
DispatchQueue.main.async {
self.showCars(true)
}
}))
self.present(viewController: alert)
}
showing = false
} else {
print("response is nil")
}
}

其中“cars”是 var cars: [Car] = [] 并且“download”的类型是 CarsToDownload 所以 download.cars是我要下载的汽车,现在我想做的是创建一个过滤器,在此函数 (getOkToDownload) 中对我从最近到最公平下载的汽车进行排序,为此我我想我必须创建一个循环来计算 carsDownloaded 的所有距离,一旦我获得了值,就用过滤器对数组进行排序,但我该怎么做呢?考虑到我通过向您展示的外部函数获取了 x 汽车的距离值,目前我仅在我的 Car 类中添加了此参数 var distanceT: Float? (don不知道是否有用)

最佳答案

您需要的实际计算只有两件事:过滤集合的距离并按距离对集合进行排序。我在 Playground 中放置了一些东西来说明这个过程。下面唯一真正的提升是两行代码:过滤和排序。

let mileMultiplier = 0.000621371

let home = CLLocation(latitude: 34.043017, longitude: -118.267254)
let loc1 = CLLocation(latitude: 32.689596, longitude: -117.999806)
let loc2 = CLLocation(latitude: 33.556666, longitude: -117.659812)
let loc3 = CLLocation(latitude: 34.043017, longitude: -118.467254)
let loc4 = CLLocation(latitude: 35.501326, longitude: -116.751808)
let loc5 = CLLocation(latitude: 33.986546, longitude: -117.454548)

let collection = [loc1, loc2, loc3, loc4, loc5]

func geoQueryCollection(collection: [CLLocation], radiusInMiles: Double, sorted: Bool) -> [CLLocation] {
let results = collection.filter { $0.distance(from: home) * mileMultiplier < radiusInMiles }
return sorted ? results.sorted { $0.distance(from: home) < $1.distance(from: home) } : results
}

let results = geoQueryCollection(collection: collection, radiusInMiles: 200, sorted: false)
results.map { print(Int($0.distance(from: home) * mileMultiplier)) }
// prints: 94, 48, 11, 132, 46

let results2 = geoQueryCollection(collection: collection, radiusInMiles: 50, sorted: true)
results2.map { print(Int($0.distance(from: home) * mileMultiplier)) }
// prints: 11, 46, 48

除了适合您需要的其他可能的改进之外,您可能还想缓存距离计算,这样就不会重复计算,但这只是起点。

关于ios - 计算从当前位置到 x 个对象的距离,并使用从最近到最远的过滤器对它们进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47636485/

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