【问题标题】:Retrieving Location Data with Radius using Firebase & GeoFire使用 Firebase 和 GeoFire 使用 Radius 检索位置数据
【发布时间】:2016-09-14 06:04:51
【问题描述】:

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

为了清楚起见,我将详细信息和位置分别保存在数据库中,因此详细信息的 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 一样,我想检索一次数据并显示,并使用“拉动刷新”功能重新检索数据。我的第一个问题是,GeoFire 是否有任何功能可以作为 Firebase 的 observeSingleEventOfType 并检索一次数据?


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

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

..但不记录任何其他内容。我不认为它进入函数内部,因为它不打印 print("Key.. 部分。也不是print("A")

我认为这可能是由“Firebase 规则”引起的。在 SO 上使用这个answer,我尝试添加这个:

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

还有这个:

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

但两者都不起作用。

更新:

使用@Gregg 的回答,我修复了警告,但是它仍然没有进入闭包内。

【问题讨论】:

    标签: ios swift firebase core-location geofire


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢!它修复了警告。但它并没有打印封闭内的东西。你知道为什么吗?我认为错误是问题所在,但显然不是。
    • 编辑:完全是我的错误。在获取核心位置之前尝试获取。非常感谢!
    • 您如何为".indexOn" 添加.?每次我尝试时都会收到一个错误,提示无法插入 . 作为键
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    • 2018-07-13
    • 2019-04-11
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多