问题
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