Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift-根据从Firebase DB提取的数据按最近位置排序_Swift_Firebase_Firebase Realtime Database_Cllocation - Fatal编程技术网

Swift-根据从Firebase DB提取的数据按最近位置排序

Swift-根据从Firebase DB提取的数据按最近位置排序,swift,firebase,firebase-realtime-database,cllocation,Swift,Firebase,Firebase Realtime Database,Cllocation,我正在尝试从firebase后端检索数据-每个条目都有用户名、lat和long。通过查看其他stackoverflow问题,我看到CoreLocation应该按照最近的位置使用我尝试使用的distancefrom(或者更确切地说,distance(From:))方法进行排序。它似乎没有按预期工作。我想知道我做错了什么 import UIKit import Firebase import FirebaseDatabase import MapKit import CoreLocation cl

我正在尝试从firebase后端检索数据-每个条目都有用户名、lat和long。通过查看其他stackoverflow问题,我看到CoreLocation应该按照最近的位置使用我尝试使用的distancefrom(或者更确切地说,distance(From:))方法进行排序。它似乎没有按预期工作。我想知道我做错了什么

import UIKit
import Firebase
import FirebaseDatabase
import MapKit
import CoreLocation

class RequestVC: UITableViewController, CLLocationManagerDelegate {

    var locationManager = CLLocationManager()
    var sellerUserNames = [String]()
    var requestLocations = [CLLocationCoordinate2D]()

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "backToMain" {
            self.navigationController?.navigationBar.isHidden = true
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()

        let databaseRef = FIRDatabase.database().reference()
        databaseRef.child("Sell_Request").observe(FIRDataEventType.childAdded, with: { (FIRDataSnapshot) in

            if let data = FIRDataSnapshot.value as? NSDictionary {
                if let name = data[Constants.NAME] as? String {
                    if let lat = data["latitude"] as? Double {
                        if let long = data["longitude"] as? Double {

                            print("\(self.sellerUserNames) Location: Latitude: \(lat), Longitude: \(long)")

                            self.sellerUserNames.append(name)
                            self.requestLocations.append(CLLocationCoordinate2D(latitude: lat, longitude: long))

                            self.tableView.reloadData()

                        }
                    }
                }
            }
        })
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sellerUserNames.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        let location = locationManager.location?.coordinate

        let buyerLocation = CLLocation(latitude: (location?.latitude)!, longitude: (location?.longitude)!)
        let sellerLocation = CLLocation(latitude: requestLocations[indexPath.row].latitude, longitude: requestLocations[indexPath.row].longitude)

        let distance = buyerLocation.distance(from: sellerLocation) / 1000

        let roundedDistance = round(distance * 100) / 100

        let distanceInMiles = roundedDistance * 0.621
        let roundedDistanceInMiles = round(distanceInMiles * 1000) / 1000


        cell.textLabel?.text = sellerUserNames[indexPath.row] + " - \(roundedDistanceInMiles) miles away"

        return cell
    }
}

你可以试着做些工作。它不适用于cocoapods,不过有一些变通方法。

您能将代码精简为一个新版本吗?你在Firebase上没有问题,你不必证明这一点。只需显示一个坐标列表,以及您希望它们在tableview中显示的顺序。请将其缩小一点,抱歉。