如何在 table 视图中列出距当前用户位置最近的用户 - iOS swift?

How to list closest user from current user location in a table view - iOS swift?

这里我正在计算用户和 driver lat long distance in km。现在我想要 table 视图中当前用户的列表 driver。目前我可以从 driver 和用户端检索经纬度并计算它们之间的距离。

这里是使用的代码:

var requestArray     = [requestModel]()
var bookRequestArray = [String]()
var DriverArray      = [String]()
var subLoaclityArray = [String]()
var LocationArray    = [String]()

var doc_ID         : String = ""
var Start_Lat      : String = ""
var Start_Long     : String = ""
var Driver_Lat     : String = ""
var Driver_Long    : String = ""

var DriverLocation : CLLocation?
var userLocation   : CLLocation?
var nearestLoaction   : CLLocationDistance?

此处获取数组中 firestore 的 driver lat long

func loadDriverDoc(){

    self.db.collection("Driver_details").getDocuments() { (querySnapshot, err) in

        if let err = err {
            print("Error getting documents: \(err)")
        } else {
            for document in querySnapshot!.documents {
                print("driverdocument::::::::::\(document.documentID) => \(document.data())")
                self.DriverArray.append(document.documentID)
                let Driver_lat = document.data()["Driver_Lat"] as? String
                print("Driver_lat:::::\(String(describing: Driver_lat))")
                self.Driver_Lat = Driver_lat!
                let Driver_long = document.data()["Driver_Long"] as? String
                print("Driver_long:::::\(String(describing: Driver_long))")
                self.Driver_Long = Driver_long!

                self.DistanceCal()


            }
            DispatchQueue.main.async {


            }


        }

    }


}

此处计算用户与driver

之间的距离
func DistanceCal() {

     DriverLocation = CLLocation(latitude: Double(Driver_Lat)!, longitude: Double(Driver_Long)!)
     userLocation   = CLLocation(latitude: Double(Start_Lat)!, longitude: Double(Start_Long)!)

    //this is the distance between driverLocation and startLocation (in km)
    let distance = (DriverLocation?.distance(from: userLocation!))!/1000

    //Display the result in km
    print(String(format: "The distance to driver is %.01fkm", distance))


    if (distance <= 1000) {



    } else if (distance <= 2000) {


    } else if (distance <= 3000) {


    } else {



    }

}//DistanceCal

此处从 firestore 获取用户数据

func loadDoc() {

    getFireBaseToken { token in


            self.db.collection("Current_booking").getDocuments() { (querySnapshot, err) in
                if let err = err {
                    print("Error getting documents: \(err)")
                } else {
                    for document in querySnapshot!.documents {
                        print("document::::::::::\(document.documentID) => \(document.data())")
                        self.bookRequestArray.append(document.documentID)
                        print("doc::::\(self.bookRequestArray)")
                        let Start_lat = document.data()["Start_Lat"] as? String
                        print("Start_lat::::::\(String(describing: Start_lat))")
                        self.Start_Lat = Start_lat!
                        let Start_long = document.data()["Start_Long"] as? String
                        print("Start_long::::::\(String(describing: Start_long))")
                        self.Start_Long = Start_long!

                         self.loadDriverDoc()
                    }
                    DispatchQueue.main.async {

                        self.requestTableView.reloadData()

                    }
                }
            }


    }//get firebase token

}//loadDoc

嗯,我建议创建一个新对象

struct Driver { 
   var docID: String!
   var location: CLLocation!
   var distanceFromUser: CLLocationDistance?
}

还在变量声明的 class 之上创建一个驱动程序数组:
var drivers = [Driver]()
您下载所有驱动程序并初始化驱动程序的结构,之后为每个驱动程序计算与当前用户之间的距离,并分配给每个驱动程序 distanceFromUser。这样做之后你应该排序

drivers.sort(by: {([=11=].distanceFromUser ?? 999999.9) < (.distanceFromUser ?? 999999.9)})
tableView.reloadData()

我给了 999999.9,因为 distanceFromUser 是可选的,如果没有计算,驱动程序将留在列表的底部。在 tableView(_:cellForRowAt:) 中,您应该访问 drivers[indexPath.row]drivers[indexPath.section],具体取决于您使用 table 视图的方式。