【问题标题】:Geofire Query in Firebase Cloud FunctionFirebase Cloud 函数中的 Geofire 查询
【发布时间】:2017-09-19 21:24:26
【问题描述】:

对不起我的英语,我怀疑是否可以在 Firebase Cloud Func 中使用它们各自的 key_enter 和 key_exited 进行查询 geofire,以维护我位置附近的地理点列表?,从几十万。考虑到当用户在节点 firebase 实时数据库中更新其地理位置时会发生所有这一切,并且这个数十万的列表减少到用户位置附近的几个

【问题讨论】:

  • Geofire 是一个构建在 Firebase JavaScript SDK 之上的库,它可以以同样的方式访问 Firebase 数据库。写入数据库是完全一样的。但是听众会以不同的方式工作。我建议您尝试一下,如果遇到困难,请回帖。

标签: firebase firebase-realtime-database google-cloud-functions geofire


【解决方案1】:

这是没有模块时的样子

let functions = require('firebase-functions');

let GeoFire = require('geofire');

exports.testLocation = functions.database.ref('/items/{item}').onWrite(event => {
    let data = event.data.val();
    console.log(data);
    console.log(event.params.item);

    if (data.location && data.location.coords) {

       console.log('Update GeoFire');

       let ref = event.data.adminRef.parent.parent.child('/items_locations'));

      let key = event.params.test;
      let location = [data.location.coords.latitude, data.location.coords.longitude]);
      let geoFire = new GeoFire(ref);

      geoFire.set(key, location).then(() => {
         console.log('Update succesfull');
   }).catch(error => {
      console.log(error);
   });

  }
}

【讨论】:

    【解决方案2】:

    以下是我使用 GeoFire 更新的技巧

    exports.testLocation = functions.database.ref('/tests/{test}').onWrite(event => {
        let data = event.data.val();
        console.log(data);
        console.log(event.params.test);
        if (data.location && data.location.coords) {
            console.log('Update GeoFire');
            geoBase = new geoBase(event.data.adminRef.parent.parent.child('/GeoFireTests'));
            geoBase.addLocation(event.params.test, [data.location.coords.latitude, data.location.coords.longitude]);
        }
    });
    

    地火

    let GeoFire = require('geofire');
    module.exports = function(ref) {
        this.geoFire = new GeoFire(ref);
        this.addLocation = (key, location) => {
            this.geoFire.set(key, location).then(() => {
                console.log('Update succesfull');
            }).catch(error => {
                console.log(error);
            });
        }
    }
    

    【讨论】:

    • 嗨乔纳斯,你能解释一下你的答案的第二部分是什么
    • 对不起,第二部分是一个单独的模块,是导入第一部分的。在这里使用:geoBase = new geoBase().
    猜你喜欢
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多