如何获取初始GeoFire值

时间:2017-08-19 15:28:05

标签: javascript firebase firebase-realtime-database geocoding geofire

geofire-js我遇到了一些问题。

我的应用中有两种不同类型的用户。用户和工人。 基本上用户发布工作和工人能够看到这些工作。但我只想向那些与当前用户关系密切的工人展示工作。

因此,在创建一个作业并将其提交给firebase后,我正在向用户客户端查询geofire以获取我周围的所有用户:

function getNearbyUsers(job) {
  let ref = firebase.database().ref('/geofire/workerUsers');

  let geoFire = new GeoFire(ref);

  let geoQuery = geoFire.query({
    center: [job.lat, job.lng],
    radius: 4
  });

  // How do I get initial data from here?

  geoQuery.on("ready", function() {
    console.log("GeoQuery has loaded and fired all other events for initial data");
  });

  geoQuery.on("key_entered", function(key, location, distance) {
    console.log(key + " entered query at " + location + " (" + distance + " km from center)");
  });

  geoQuery.on("key_exited", function(key, location, distance) {
    console.log(key + " exited query to " + location + " (" + distance + " km from center)");
  });

  geoQuery.on("key_moved", function(key, location, distance) {
    console.log(key + " moved within query to " + location + " (" + distance + " km from center)");
  });
}

现在在我看来,我只能听到移动到te查询区域的键以及离开它的人。那很好。

但我如何获得初始数据? (来自查询区域内的用户)

这是我在数据库中有一个虚拟WORKER用户的图片,显然是查询区域。

enter image description here

我很抱歉,如果它很明显,我错过了一些东西。

1 个答案:

答案 0 :(得分:1)

创建地理查询时,key_entered事件将立即触发范围内的每个键。

documentation确实没有明确提到这一点。但是当你运行代码时,你会发现它会立即为你提供相关的密钥。