使用Firebase(GeoFire)和Swift无法按我的位置靠近用户 [英] Can't get near Users by my location with Firebase (GeoFire) and Swift

查看:36
本文介绍了使用Firebase(GeoFire)和Swift无法按我的位置靠近用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让所有用户都位于我自己的位置附近.

I want to get all Users near by my own Location.

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

I wrote following code in the location Manager didUpdateLocation function.

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)

然后我进行查询以使所有用户都位于我所在的位置附近.

Than I made a query to get all users near by my location.

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)'")
                
                
                
            })

如果我现在想替换"test",与我的实际用户(userdocid)一起无法使用.

If I now want to replace "test" with my actual user (userdocid) it won't work.

我如何抓住我的用户:

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,应用程序崩溃了.

The App is crashing because of empty NSArray.

预先感谢您标记

推荐答案

此答案取决于您如何存储用户数据,但是如果您存储的用户具有其documentId作为用户uid的信息,那么

This answer depends on how you're user data is stored but if you're storing users with their documentId as the users uid then just

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

然后

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

但是,如果您想像在问题中一样获得它

however, if you want to get it as you're doing in the question

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和子字段中,这是不必要的:

But again, that's a bit redundant as it wound indicate storing the uid in both the documentId as well as a child field, which is unnecessary:

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

该问题似乎实际上是在获取中心点-例如如果未存储,则在读取时将为空,并且会出现该错误.

The problem appers to be actually getting the center point - e.g. if it's not stored, then when read, it will be empty and you'll get that error.

所以您必须先存储它

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!")
  }
}

一旦成功存储了中心点(您的位置),然后对其进行了检索,则实际的查询应该是

Once you successfully stored a center point (your location), and then retrieved it, then the actual query should be

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

哦,非常重要的一点是setLocation是异步的;服务器存储数据需要花费时间.您确实应该使用闭包中的数据

Oh, and a very important thing is the setLocation is asynchronous; it takes time for the server to store the data. You should really be working with the data within the closure

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

这篇关于使用Firebase(GeoFire)和Swift无法按我的位置靠近用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆