swift - 如何将附近的位置设置到从 Firebase 检索的 TableView 中

标签 swift dictionary firebase firebase-realtime-database latitude-longitude

我是 Swift 3 的新手,我有一个问题是关于确定相对于用户位置的坐标是否在附近。之后我想将附近的位置显示到 TableView 中。

这是我的 Firebase 结构:

enter image description here

我的类(class)是这样的:

class UITableViewControllerForShop: UIViewController, UITableViewDelegate, UITableViewDataSource, CLLocationManagerDelegate

这些变量是全局的:

let databaseRef = FIRDatabase.database().reference().child("Butikker")
var retrieveData = [String:AnyObject]()
var distanceInMeters: CLLocationDistance!

在我的 viewDidLoad 函数中,我执行以下操作:

override func viewDidLoad() {
        super.viewDidLoad()

        databaseRef.observeSingleEvent(of: .value, with: { (snapshot) in
            for child in snapshot.children {
                let snap = child as! FIRDataSnapshot
                let dictionary = snap.value as! [String: AnyObject]

                self.retrieveData = ["latitude": dictionary["Latitude"] as! Double as AnyObject, "longtitude": dictionary["Longtitude"] as! Double as AnyObject]
            }
        })
}

要检查用户位置并将其与 Firebase 位置进行比较,我执行以下命令:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations[0]

    print(location.coordinate.latitude, location.coordinate.longitude)

    //My location
    let myCoordinate = CLLocation(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)

    if self.retrieveData.isEmpty == false {
        let shopCoordinate = CLLocation(latitude: retrieveData["latitude"]! as! CLLocationDegrees, longitude: retrieveData["longtitude"]! as! CLLocationDegrees)
        print(shopCoordinate.coordinate.latitude, shopCoordinate.coordinate.longitude)
        distanceInMeters = myCoordinate.distance(from: shopCoordinate)
        print(distanceInMeters)
    }
    //Distance
}

我像这样初始化我的表格 View :

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!

    if distanceInMeters > 5000 {
        cell.textLabel?.text = retrieveData["latitude"] as! String
    }
    return cell
}

这些是我遇到的问题:

  1. 在 locationManager 函数中初始化后,我不知道如何检查我的距离是否> 5000。
  2. 我的字典检索数据不会为每个键存储超过 1 个值,如果我想在表格 View 中显示附近项目的列表,这会导致出现问题。
  3. 执行 for in 循环需要时间,这使得一切都变得更加困难。

我想得到答案的问题:

1.在locationManager函数中初始化distanceInMeters后如何使用?

2.如何判断附近是否有东西并放入我的TableView中?

最佳答案

如果您想实现这一目标,我建议您使用一个完全满足您需要的库:https://github.com/firebase/geofire-objc

我想你可以自己写这个,但这会花费很多时间,因为 Firebase 不会帮助你处理与 GeoLocation 相关的任何功能(如果你将它与 Parse 服务(已死)进行比较,后者已经包含了要查询的功能按距离定位)。

关于swift - 如何将附近的位置设置到从 Firebase 检索的 TableView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45580654/

相关文章:

如果键不存在,则使用 setter 的 C# 自定义字典类型

python - 获取字典中具有第二个和第三个最大值的键

javascript - firebase上托管的node.js/express.js无法导出数据库和身份验证监听器

ios - 如何为 Swift 实现 AVCam

python:从txt文件的行创建字典

javascript - 在 firebase 中查找集合长度

java - 为什么 Firebase 数据库上的循环会一次又一次地添加数据?

swift - 车轮沉入地板 - Scenekit(IOS)

ios - TextView 点按在 iOS 14 上不起作用

ios - 如何在 Swift 中使用 CAShapeLayer 为 CALayer.SubLayer 创建橡皮擦