MichaelSolati / geofirestore-js

Location-based querying and filtering using Firebase Firestore.
https://geofirestore.com
MIT License
505 stars 58 forks source link

Get all documents within a radius #43

Closed 24dev closed 6 years ago

24dev commented 6 years ago

Trying to retrieve all the documents that fall within the radius of a certain point.

Query:

const geoQuery = geoFirestore.query({
    center: new firebase.firestore.GeoPoint(51.5074, 0.1278),
    radius: 5,
  });

  geoQuery
    .query()
    .get()
    .then(querySnapshot => {
      querySnapshot.forEach(doc => {
        console.log(doc.data());
      });
    })

Firestore (documents set by geofirestore.set(): Document 1 (same as search point):

coordinates: [51.5074, 0.1278]

Document 2:

coordinates: [41.1537, -8.612549]

No matter what I try, it is always returning both results, even though they are 100's of kms away from eachother. Can someone please recommend what I can do to make this work?

Much appreciated! :)

MichaelSolati commented 6 years ago

That seems odd, and there are test in place for just this sort of issue. Could you provide a working (broken) example?

24dev commented 6 years ago

@MichaelSolati Haven't got anything live, but what extra info would you like to see?

This is my collection:

screen shot 2018-10-09 at 17 59 51

After making the request as shown above, I get this response:

screen shot 2018-10-09 at 18 02 58

+++++

This is my firebase config, if useful:

firebase.initializeApp(firebaseConfig);
firebase.firestore().settings({ timestampsInSnapshots: true });
const db = firebase.firestore();
const geostoreRef = db.collection('exploreMap');

// Create a GeoFirestore index
const geoFirestore = new GeoFirestore(geostoreRef);
MichaelSolati commented 6 years ago

In all sincerity a broken app would be helpful. I'm doing tests and fixes right now for other issues and have not seen this issue. So an example would be preferred. It can be simple, it just need to be broken.

MichaelSolati commented 6 years ago

Ok, so I just took a better look at your code, and I feel really silly not to have realized this before, but I do not think you are using the library correctly. In fact I'm surprised you're getting anything at all the way you are using it.

The below code creates a GeoFirestoreQuery:

const geoQuery = geoFirestore.query({
  center: new firebase.firestore.GeoPoint(51.5074, 0.1278),
  radius: 5,
});

If you wanted to make a geoquery you'd use the on listener for the key_entered event which will return documents in your query, see here.

However you're calling the query function, which returns a Firestore Query, or CollectionReference (depending on if you passed in a query function when you created or updated the query criteria).

Calling get on this query BYPASSES all of the GeoFirestore magic goodness, and would not provide you with what you want or expect... Instead you'd want to do something like this.

// Store all results from geoqueries here
let results = [];

// Create geoquery
const geoQuery = geoFirestore.query({
  center: new firebase.firestore.GeoPoint(51.5074, 0.1278),
  radius: 5,
});

// Remove documents when they fall out of the query
geoQuery.on('key_exited', ($key) => {
  const index = results.findIndex((place) => place.$key === $key);
  if (index >= 0) results.splice(index, 1);
});

// As documents come in, add the $key/id to them and push them into our results
geoQuery.on('key_entered', ($key, result) => {
  result.$key = $key;
  results.push(result);
});

I'll be closing this for now, as I think I have your issue/question addressed. However please let me know if I am mistaken and we can reopen this and go from there.

24dev commented 6 years ago

Brilliant, that fixed it. Sorry for my misunderstanding! The key_entered/exited is what confused me. Thanks for the great library!

stevenyix commented 6 years ago

i also had the same issue as @AlcuinGuest. finding this issue and explanation solved my dilemma as well. thank you for providing the info and great tools.

food for thought: when i read the docs i interpreted 'key_entered' and 'key_exited' as some type of keyboard events for a browser client-side scenario. i'm also fairly new to node.js. nonetheless, i was about to give up until i found this. thanks.