在Swift中使用GeoFire查询不会提供有用的输出

我现在非常绝望,因为我正试图在我的Firebase数据库上使用GeoFire来查找附近的用户。 我现在已经被困了两天了。 我搜索了谷歌和stackoverflow很多,并尝试了我在那里找到的所有东西,但没有任何成功。 现在我最后的希望是自己创建这个post,希望有人能帮助我。

我的数据库看起来像这样:

users user_id1 email: xxx@gmail.com username: xxx user_id2 email: yyy@gmail.com username: yyy users_locations user_id1 location g: xxxx l 0: 30.0000 1: 5.0000 user_id2 location g: yyyy l 0: 30.0010 1: 5.0010 

我不知道是否有必要保存与用户分离的位置数据,但我也试图像这样保存它,结果相同,所以我觉得没关系:

 users user_id1 email: xxx@gmail.com username: xxx location g: xxx l 0: 30.0000 1: 5.0000 

现在我正在使用的代码:

要将位置写入数据库,我使用以下,这是正常工作:

 let user = FIRAuth.auth()?.currentUser let uid = user?.uid let ref = FIRDatabase.database().reference() let geofireRef = ref.child("users_locations") let geoFire = GeoFire(firebaseRef: geofireRef.child(uid!)) geoFire?.setLocation(myLocation, forKey: "location") 

现在我尝试使用GeoFire查询向我显示定义半径内的所有用户,这些用户不会为此打印任何内容:

 let ref = FIRDatabase.database().reference() let geofireRef = ref.child("users_location") let geoFire = GeoFire(firebaseRef: geofireRef) let circleQuery = geoFire?.query(at: center, withRadius: 10) circleQuery?.observe(.keyEntered, with: { (key: String?, location: CLLocation?) in print("Key '\(key!)' entered the search are and is at location '\(location!)'") }) 

我想到如果我去找我真正的孩子,我会得到一个结果。 然后它打印我的实际用户位置和密钥,但这当然不是我想要的。

 let geoFire = GeoFire(firebaseRef: geofireRef.child(uid!)) 

所以我希望GeoFire在’users_locations’中搜索我的uid,然后返回我定义的半径内的所有uid。

对我来说,似乎它只是在我的Reference(users_location – > user_uid)中定义的子项中搜索名为’location’的子项,如果我尝试查询’users_location’,我什么也得不到。

我究竟做错了什么? 如何让查询搜索引用子项并将其返回给我?

尝试使用GeoFire这样写位置。 您无需将它们存储在该location键下。

 let ref = FIRDatabase.database().reference() let geofireRef = ref.child("users_locations") let geoFire = GeoFire(firebaseRef: geofireRef) geoFire?.setLocation(myLocation, forKey: uid!) 

使用Geofire时,您有两个数据列表:

  1. 对象列表,在您的情况下是用户
  2. 这些对象的位置列表,通过Geofire进行维护和查询

这两个清单确实是分开的。 从Geofire文档 (强调我的):

假设您正在构建一个应用程序来评估条形图,并且您存储条形图的所有信息,例如名称,营业时间和价格范围,位于/bars/ 。 之后,您希望为用户添加搜索附近酒吧的可能性。 这就是GeoFire的用武之地。

您可以使用GeoFire存储每个柱的位置, 使用条形码ID作为GeoFire键 。 然后,GeoFire允许您轻松查询附近的条形码ID(键)。 要显示有关条形的任何其他信息,您可以在/bars/加载查询返回的每个条形图的信息

我强调了你问题的两个最重要的部分:

  1. 用户列表中的项目及其位置列表应使用相同的密钥(这也是安德鲁在他的回答中指出的)。 通过使用相同的密钥,您可以轻松地查找用户的位置,反之亦然。
  2. 您需要为查询中的每个键单独加载用户。

Andrew展示了如何正确地为每个用户编写位置。 剩下的就是加载关于结果范围内的每个用户的附加信息。 你可以在.keyEntered处理程序中执行此.keyEntered

 let usersRef = ref.child("users") let circleQuery = geoFire?.query(at: center, withRadius: 10) circleQuery?.observe(.keyEntered, with: { (key: String?, location: CLLocation?) in print("Key '\(key!)' entered the search are and is at location '\(location!)'") // Load the user for the key let userRef = usersRef.child(key) userRef.observeSingleEvent(FIRDataEventType.value, with: { (snapshot) in let userDict = snapshot.value as? [String : AnyObject] ?? [:] // ... }) })