【问题标题】:Can't get near Users by my location with Firebase (GeoFire) and Swift使用 Firebase (GeoFire) 和 Swift 无法靠近我所在位置的用户
【发布时间】:2021-02-22 19:13:57
【问题描述】:

我想让所有用户都靠近我自己的位置。

我在位置管理器 didUpdateLocation 函数中编写了以下代码。

First I grabbed my own location as a CCLocationCoridnate2D.
if let location = locations.last{ let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)

然后我进行了查询以获取我所在位置附近的所有用户。

let geofireRef = Database.database().reference()
                let geoFire = GeoFire(firebaseRef: geofireRef)
            
            
            
            
            
            geoFire.setLocation(CLLocation(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude), forKey: "test")
          
         
            let centerQuery = CLLocation(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
            let circleQuery = geoFire.query(at: centerQuery, withRadius: 5)

            var queryHandle = circleQuery.observe(.keyEntered, with: { (key: String!, location: CLLocation!) in
               print("Key '\(key)' entered the search area and is at location '\(location)'")
                
                
                
            })

如果我现在想用我的实际用户 (userdocid) 替换“test”,它将无法正常工作。

我如何抓住我的用户:

Firestore.firestore().collection("users").whereField("uid", isEqualTo: Auth.auth().currentUser?.uid ?? "x")
                            .getDocuments { (querySnapshot, err) in
                                if err != nil {
                                    print("Failed")
                                    return
                                }
let  userdocid = querySnapshot!.documents[0].documentID

由于 NSArray 为空,应用程序正在崩溃。

提前谢谢你 标记

【问题讨论】:

    标签: swift firebase google-cloud-firestore geofire


    【解决方案1】:

    这个答案取决于您存储用户数据的方式,但如果您使用他们的 documentId 作为用户 uid 存储用户,那么只需

    let uid = Auth.auth().currentUser!.uid //should safe unwrap that optional
    

    然后

    geoFire.setLocation(CLLocation(..., forKey: uid)
    

    但是,如果您想像在问题中那样得到它

    let uid = Auth.auth().currentUser!.uid
    let usersCollection = Firestore.firestore().collection("users")
                                   .whereField("uid", isEqualTo: uid)
                            
    usersCollection.getDocument(completion: { documentSnapshot, error in
        if let err = error {
             print(err.localizedDescription)
             return
         }
    
         guard let docs = documentSnapshot?.documents else { return }
    
         let thisUserUid = docs[0].documentID
         print(thisUserUid)
    })
    

    但同样,这有点多余,因为它表明将 uid 存储在 documentId 和子字段中,这是不必要的:

    users
      uid_0 //the documentId
         name: "Users name"
         uid: "uid_0" //not needed
    

    问题出现在实际获得中心点 - 例如如果它没有被存储,那么当它被读取时,它将是空的,你会得到那个错误。

    所以你必须先存储它

    geoFire.setLocation(CLLocation(latitude: 37.7853889, longitude: -122.4056973), forKey: uid) { (error) in
      if (error != nil) {
        print("An error occured: \(error)")
      } else {
        print("Saved location successfully!")
      }
    }
    

    一旦你成功存储了一个中心点(你的位置),然后检索它,那么实际的查询应该是

    let center = CLLocation(userLocation)
    geoFire.queryAtLocation(center, withRadius: 20);
    

    哦,还有很重要的一点是 setLocation 是异步的;服务器存储数据需要时间。你真的应该使用闭包中的数据

    geoFire.setLocation(CLLocation(latitude: 37.7853889, longitude: -122.4056973), forKey: uid) { (error) in
       // data is now valid so perform your query
    }
    

    【讨论】:

    • 但问题是如何从远处抓取用户,而不是如何从数据库中抓取用户
    • @Markus 问题是这样 如果我现在想用我的实际用户 (userdocid) 替换“test”,它将不起作用。 因为这个 由于 NSArray 为空,应用程序正在崩溃。 所以必须首先解决这个问题。我们不知道应用程序在哪里崩溃,因为问题中没有说明故障排除;所以我们在这一点上猜测。另请参阅更新后的答案。
    • @Markus 我在回答完之前点击了保存。我提供了一些可能有帮助的额外信息。
    • 我不想自己设置位置。我想使用 CC 位置管理器。谢谢
    • 所以所有在 CC 位置管理器中...获取我自己的位置,然后在此时设置它
    猜你喜欢
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多