使用Firebase&& GeoFire [英] Retrieving Location Data with Radius using Firebase & GeoFire

查看:195
本文介绍了使用Firebase&& GeoFire的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用GeoFire来检索和根据半径(例如,在10公里的距离内的一切)。

为了保持清晰,我将数据库中的详细信息和位置分开保存,所以详细信息的id与位置id相同。在保存数据的同时,我使用:

pre code var details = [a:a,b:b 等等]
让checkpointRef = eventRef.childByAutoId()
checkpointRef.setValue(details)
let checkpointId = checkpointRef.key

let geofireRef = ref.childByAppendingPath (checkpointLocations)
让geoFire = GeoFire(firebaseRef:geofireRef)

geoFire.setLocation(CLLocation(latitude:usersLatitude,longitude:userLongitude),forKey:checkpointId){...

直到这里,数据库中的所有内容都看起来不错。



< hr>

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

  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)

printQuery(Key\\)中的circleQuery.observeEventType(.KeyEntered,withBlock:{(key:String !, location:CLLocation! \\(键)'进入搜索区域,位于'\(位置)')
})

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






其次,这个函数打印这个警告几次:


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

..但是不记录还要别的吗。我不认为它进入函数内部,因为它不会打印 print(Key .. ),也不会打印 print (A)



我认为这可能是由于Firebase规则导致的。使用

 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:{
$ b $}
}
}


I am trying to use GeoFire to retrieve and according to radius (for example, everything in 10km distance).

To remain clear, I am saving the details and location separately in the database, so details' id is same with locations' id. While saving the data, I use:

 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) {...

Until here, everything seems fine in database.


While retrieving data, after getting user's latitude and longitude from core location, I am trying to :

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

First problem arises here with observeEventType(). I don't want to observe event continuously. Instead, like a classic TableView, I want to retrieve the data once and display, and re-retrieve data with "pull to refresh" functionality. My first question is, are there any function for GeoFire to work as observeSingleEventOfType of Firebase and retrieve data once?


Secondly, this function is printing this warning couple of times:

[Firebase] Using an unspecified index. Consider adding ".indexOn": "g" at /checkpointLocations to your security rules for better performance

..but doesn't log anything else. I don't think it goes inside the function because it doesn't print the print("Key.. part. It doesn't also print("A").

I thought it might be caused because of the 'Firebase Rules'. Using this answer on SO, I tried adding this:

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

and also this:

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

But neither works.

Update:

Using @Gregg's answer, I fixed the warnings, however, it still doesn't go inside the closure.

解决方案

Dont have enough points for commenting but move the ".indexOn" up to the checkpointLocations node. Currently you have it on checkpointLocations/geofire. For example:

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

      }
    }
  }

这篇关于使用Firebase&amp;&amp; GeoFire的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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