如何获得最初的GeoFire值 [英] How to get initial GeoFire values

查看:139
本文介绍了如何获得最初的GeoFire值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的



我很抱歉,如果它明显,我失踪当你创建你的地理查询时, key_entered 事件

即时触发范围内的每个键。

documentation 的确没有明确提到这一点。但是当你运行这个代码的时候,你会发现它会立即给你相关的键。


I am having a bit of a problem with geofire-js.

I have two different kind of users in my app. Users and Workers. Basically users post Jobs and workers are able to see those jobs. But I only wanna show jobs to workers that are close to the current user.

So after creating a job and submitting it to firebase I am querying from the user client to geofire to fetch me all the users around him:

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

Now it appears to me that I can only listen to keys that moved into te query area and to those who leave it. That is great.

But how do I get the INITIAL DATA? (from the users already within the query area)

This is a picture of some dummy WORKER user I have in the db, that is clearly withing the query area.

I am sorry if its evident and I am missing something out.

解决方案

When you create your geoquery, the key_entered event immediate fires for each key already in the range.

The documentation indeed doesn't mention this explicitly. But when you run the code, you'll see that it gives you the relevant keys straight away.

这篇关于如何获得最初的GeoFire值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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