在Swift中使用GeoFire查询不会提供有用的输出 [英] Using GeoFire queries in Swift does not give useful output

查看:64
本文介绍了在Swift中使用GeoFire查询不会提供有用的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在非常绝望,因为我试图在Firebase数据库上使用GeoFire来查找附近的用户. 我已经困了两天了. 我在Google和stackoverflow上进行了很多搜索,并尝试了在该网站上找到的所有内容,但均未成功.现在,我的最后希望是自己创建一个线程,希望有人可以帮助我.

I'm pretty desperate right now because I'm trying to use GeoFire on my Firebase Database to find nearby users. I'm pretty much stuck for two days now. I searched Google and stackoverflow a lot and tried everything I found there but without any success. Now my last hope is to create this thread myself and hope someone can help me.

我的数据库几乎是这样的:

My database pretty much looks like this:

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

我不知道是否有必要保存与用户分开的位置数据,但我也尝试用相同的结果保存它,所以我认为这没关系:

I don't know if it is necessary to save the location data seperated from the users but I also tried to save it like this with the same results so I think it doesn't matter:

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

现在我正在使用的代码:

Now the code I'm using:

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

To write the location into the database I use the following, which is working fine:

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查询向我显示处于定义半径内的所有用户,该用户对此没有任何打印内容:

Now I try to use a GeoFire Query to show me all users in a defined radius which is printing nothing for this one:

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

我发现,如果我去了我的真正的uid孩子,我会得到结果.然后打印出我的实际用户位置和密钥,但这当然不是我想要的.

I figured out that I get a result if I go to my actual uid child. Then it print my actual users location and key but that's of course not what I want to have.

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

因此,我希望GeoFire在'users_locations'中搜索我的uid,并向我返回所有在我定义的半径内的uid.

So I want GeoFire to search through my uid's in 'users_locations' and return me all uid's which are in my defined radius.

在我看来,这似乎只是在我的参考文献(users_location-> user_uid)中定义的子级中搜索名为"location"的子级,如果我尝试通过"users_location"进行查询,则一无所获.

To me, it seems like it is only searching for the child named 'location' in the child defined in my Reference (users_location -> user_uid) and if I try to query over 'users_location', I get nothing.

我做错了什么? 如何让查询搜索引用子对象并给我uid?

What am I doing wrong? How can I make the query search reference child and give me the uid back?

推荐答案

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

Try writing the locations with GeoFire like this. You don't need to store them under that location key.

let ref = FIRDatabase.database().reference()
let geofireRef = ref.child("users_locations")
let geoFire = GeoFire(firebaseRef: geofireRef)

geoFire?.setLocation(myLocation, forKey: uid!)

这篇关于在Swift中使用GeoFire查询不会提供有用的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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