firebase / geofire-js

GeoFire for JavaScript - Realtime location queries with Firebase
MIT License
1.44k stars 345 forks source link

How to get a query response without any event on GeoFire? #239

Open bsgies opened 2 years ago

bsgies commented 2 years ago

I'm using GeoFire to get the closest point from the center within a certain radius. I have shared my codebase below.

The issue I'm having is finding the closest point without any event. I have read the documentation which says:

There are five kinds of "key" events that can occur with a geo query:

Key Entered: The location of a key now matches the query criteria. Key Exited: The location of a key no longer matches the query criteria. Key Moved: The location of a key changed but the location still matches the query criteria. Query Ready: All current data has been loaded from the server and all initial events have been fired. Query Error: There was an error while performing this query, e.g. a violation of security rules.

I'm looking to query without any event. For example, I have point A at (51.59141592849507, 0.00877643935500006), point B at (51.593452498310185, 0.005547504234921514), and point C at (51.583411841367415, 0.007768887829733491).

What should I do if these points don't enter, exit, or move to or from the center? I still need to know their distance from the center.

var admin = require("firebase-admin");
var geofire = require('geofire');

// Fetch the service account key JSON file contents
var serviceAccount = require("service-account.json");

// Initialize the app with a service account, granting admin privileges
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  // The database URL depends on the location of the database
  databaseURL: "https://cnf-terseel-05-default-rtdb.firebaseio.com"
});

var db = admin.database();
var ref = db.ref("partners_location");

var ref2 = new geofire.GeoFire(ref);

var geoQuery = ref2.query({
 center: [-16.080257, 153.605347],
 radius: 8
});
geoQuery.on("key_entered", function(key, location, distance) {
 console.log(key, location, distance);
});