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

时间:2018-04-18 08:33:53

标签: ios swift xcode google-cloud-firestore cllocationmanager

这里我用km计算用户和司机lat长距离。现在我想从表格视图中的用户当前列表驱动程序。目前我能够从驾驶员和用户端检索lat long并计算它们之间的距离。

这是使用的代码:

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获取驱动程序lat
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 {


            }


        }

    }


}

这里计算用户和驱动程序之间的距离

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

1 个答案:

答案 0 :(得分:1)

我建议创建一个新对象

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

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

drivers.sort(by: {($0.distanceFromUser ?? 999999.9) < ($1.distanceFromUser ?? 999999.9)})
tableView.reloadData()

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