Geofire Query in Firebase Cloud Function

与世无争的帅哥 提交于 2019-12-24 01:47:28

问题


Sorry for my english, I have a doubt it is possible to make a query geofire in Firebase Cloud Func, with their respective key_enter and key_exited, in order to maintain a list of the geographic points near my position ?, extracted from a few hundred thousand. Taking into account that all this happens when the user updates its geolocation in a node firebase realtime database, and this list of hundreds of thousands is reduced to a few near the position of the user


回答1:


The following is a trick I used to update with 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);
        });
    }
}



回答2:


This is how it would look like without the module

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

  }
}


来源:https://stackoverflow.com/questions/43567031/geofire-query-in-firebase-cloud-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!