Firebase云功能中的Geofire查询

时间:2017-04-23 03:22:01

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

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

2 个答案:

答案 0 :(得分:0)

以下是我用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]);
    }
});

GeoFire

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

答案 1 :(得分:0)

这是没有模块的情况

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

  }
}