我現在非常絕望,因爲我試圖在我的Firebase數據庫上使用GeoFire來查找附近的用戶。 我現在已經呆了兩天了。 我搜索谷歌和stackoverflow了很多,並嘗試了我在那裏發現的一切,但沒有任何成功。現在我最後的希望是自己創建這個線程,希望有人能幫助我。在Swift中使用GeoFire查詢並沒有給出有用的輸出
我的數據庫幾乎是這樣的:
users
user_id1
email: [email protected]
username: xxx
user_id2
email: [email protected]
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: [email protected]
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通過我的UID在「users_locations」搜索並返回我所有的UID這是在我的定義半徑。
對我來說,它似乎只是在我的引用(users_location - > user_uid)中定義的子級中搜索名爲'location'的子級,並且如果我嘗試通過'users_location'查詢,我什麼都得不到。
我在做什麼錯? 我該如何使查詢搜索引用孩子,並給我回uid?
你在哪裏定義'center':你應該這樣做在他
.keyEntered
處理? –我將中心設置在我的實際位置之前。我嘗試了一點,但在這個例子中,它是我的位置 – user3389960
並且是''users_locations'中位於距'center中心10公里內的位置?因爲否則他們不會出現。 –