如果在半径区域内没有索引,让GeoFire返回null

Get GeoFire to return null if there is no index within radius zone

本文关键字:索引 GeoFire null 返回 区域 如果      更新时间:2023-09-26

我正在用angularjs和firebase构建一个web应用程序。我使用geofire来返回在给定区域的给定半径内的值,lng。它是完全工作,并返回我想要的,当有值的半径。但是,当半径区域中没有值时,它将不返回任何值。这是一个问题,因为在初始页面加载我有一个旋转器,开始和结束时,在一个承诺中返回的东西。是否有一种方法,让geofire返回一个空值,如果没有值在给定的半径?

这是我用来检索数据的代码:

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

  console.log(key);
      },function(error) {
        console.log("Promise was rejected with the following error: " + error);
      });
    });

console.log将不会运行,除非在radius区域中找到一些东西。我需要它仍然返回一些东西,这样我就知道提醒用户在给定的区域没有企业。任何帮助将非常感激!

更新:

我也添加了'ready'事件,认为它会触发'key_entered'事件,但它并没有因此让我回到起点。

var onKeyExitedRegistration = geoQuery.on("key_exited", function(key, location, distance) {

console.log(键+ "退出查询到"+ location + "(";+距离+ "距离中心Km)";});

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

});

key_entered事件只会在一个键进入地理查询时触发。因此,如果没有键输入查询,它将永远不会触发。

要检查是否有键最初在地理查询中,您可以侦听ready事件。在加载完初始数据后触发,因此:

var keysEntered = false;
geoQuery.on("key_entered", function(key, location, distance) {
    console.log(key + " entered query at " + location + " (" + distance + " km from center)");
    keysEntered = true;
  },function(error) {
    console.log("Promise was rejected with the following error: " + error);
  });
});
geoQuery.on("ready", function() {
  console.log("initial data has loaded");
  if (!keysEntered) {
    console.log("There was no initial data, so there are no business in range (yet).");
  }
});

查看Geofire for JavaScript的API参考。