使用Firebase和GeoFire以Radius检索位置数据

我正在尝试使用GeoFire检索并根据半径(例如,距离10km的所有内容)。

为了保持清晰,我在数据库中分别保存了细节和位置,所以细节的id与位置的id是一样的。 在保存数据的同时,我使用:

var details = ["a":"a", "b":"b", etc] let checkpointRef = eventRef.childByAutoId() checkpointRef.setValue(details) let checkpointId = checkpointRef.key let geofireRef = ref.childByAppendingPath("checkpointLocations") let geoFire = GeoFire(firebaseRef: geofireRef) geoFire.setLocation(CLLocation(latitude: usersLatitude, longitude: userLongitude), forKey: checkpointId) {... 

在这之前,数据库中的一切似乎都很好。


在获取数据的同时,从核心位置获取用户的纬度和经度后,我试图:

 func retrieve() { let geofireRef = Firebase(url: self.baseurl + "/checkpointLocations") let geoFire = GeoFire(firebaseRef: geofireRef) let center = CLLocation(latitude: usersLatitude, longitude: usersLongitude) var circleQuery = geoFire.queryAtLocation(center, withRadius: 10) circleQuery.observeEventType(.KeyEntered, withBlock: { (key: String!, location: CLLocation!) in print("Key '\(key)' entered the search area and is at location '\(location)'") }) 

这里第一个问题出现在observeEventType() 。 我不想继续观察事件。 相反,像一个经典的TableView,我想要检索一次数据,并显示,并重新检索数据与“拉刷新”function。 我的第一个问题是,GeoFire是否有任何function可以像observeSingleEventOfType的observeSingleEventOfType一样工作并检索一次数据?


其次,这个function是打印这个警告的几次:

[Firebase]使用未指定的索引。 考虑在/ checkpointLocations添加“.indexOn”:“g”到您的安全规则以获得更好的性能

..但不logging其他东西。 我不认为它进入function,因为它不打印的print("Key..部分。它不print("A")

我认为这可能是由于“Firebase规则”造成的。 在SO上使用这个答案 ,我试着添加这个:

  "rules": { "checkpointLocations": { "$key": { ".read": true, ".write": true, "geofire": { ".indexOn": "g" } } } } 

还有这个:

  "rules": { "checkpointLocations": { ".read": true, ".write": true, "geofire": { ".indexOn": "g" } } } 

但是都没有用。

更新:

用@格雷格的答案,我解决了警告,但是,它仍然没有进入封闭。

没有足够的评论点,但将“.indexOn”移动到checkpointLocations节点。 目前,你已经在checkpointLocations / geofire。 例如:

 "rules": { "checkpointLocations": { ".read": true, ".write": true, ".indexOn": "g" "geofire": { } } }