斯威夫特 - 从数据排序由最近的位置,从火力地堡DB拉(Swift - Sorting by Nea

2019-09-29 02:30发布

我想从我的火力后台检索数据 - 每个条目都有一个用户名,lat和长。 从寻找其他计算器的问题,我看到CoreLocation应该是最近的位置排序与它的distancefrom(或者更确切地说,距离(:))的方法,我试图使用。 它似乎并不打算工作。 我不知道我在做什么错。

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
    }
}

Answer 1:

你可以尝试做让GeoFire工作。 它不具有的CocoaPods工作,但也有变通方法。



文章来源: Swift - Sorting by Nearest Location from Data pulled from Firebase DB