【问题标题】:Calculate the distance from current location to x objects and orders them with a filter from the nearest to the furthest计算从当前位置到 x 个对象的距离,并使用过滤器从最近到最远对它们进行排序
【发布时间】:2017-12-04 15:19:01
【问题描述】:

我创建了两个函数来计算从我的位置到 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")
    }
}

其中“汽车”是var cars: [Car] = [],“下载”是CarsToDownload 类型,所以download.cars 是我要下载的汽车,现在我要做的是创建一个按顺序排列的过滤器这个函数 (getOkToDownload) 我从最近下载到最公平的汽车,为此我想我必须创建一个循环来计算 carsDownloaded 的所有距离,一旦我获得了值,用 a 对数组排序过滤,但我该怎么做?考虑到我用我展示给你的那些外部函数得到了一辆 x 车的距离值,现在我只在我的Car 类中添加了这个参数var distanceT: Float?(不知道它是否有用)

【问题讨论】:

    标签: ios arrays swift for-loop


    【解决方案1】:

    您需要的实际计算只是两件事:根据距离过滤您的集合并按距离对该集合进行排序。我在 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
    

    除了适合您需要的其他可能的改进之外,您可能希望缓存距离计算,以免重复计算,但这是起点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-13
      • 1970-01-01
      • 2017-03-16
      • 1970-01-01
      • 1970-01-01
      • 2016-10-27
      相关资源
      最近更新 更多