2

What I'm trying is to get the documents that are in range of a point.

Following the videos and taking a look to the geo-hash library for android, I'm able to get the bounding box and get the necessary geohashes to query in firebase.

As example:

Point -> LatLng(40.4378698,-3.8196205) (Madrid,Spain)

Radius -> 5000meters (5 km)

The boundary box I get is:

[GeoHashQuery{startValue='ezjnh', endValue='ezjns'}, GeoHashQuery{startValue='ezjjs', endValue='ezjj~'}, GeoHashQuery{startValue='ezjq0', endValue='ezjq8'}, GeoHashQuery{startValue='ezjm8', endValue='ezjmh'}]

One I have this list, I call Firebase to retrieve the documents that "match" this criteria:

fun getUpTos(queries: MutableSet<GeoHashQuery>, onSuccessListener: OnSuccessListener<QuerySnapshot>, onFailureListener: OnFailureListener) {
        var reference = Firebase.firestore.collection("pois")
        queries.forEach { entry ->
            reference
                .whereGreaterThanOrEqualTo("geohash", entry.startValue)
                .whereLessThanOrEqualTo("geohash", entry.endValue)
        }

        reference.get()
            .addOnSuccessListener(onSuccessListener)
            .addOnFailureListener(onFailureListener)
    }

At this moment I have around 20 Poi's in firebase to start doing the test. All Poi's are in Barcelona and 1 in Madrid.

After doing the query, I'm gettin ALL the poi's, when it was supposed to just return the Madrid poi.

How can I get only the pois that fit the query? It seems is not working properly (or I'm doing obviously something wrong)

Is possible to achieve this type of querys?

Shudy
  • 7,806
  • 19
  • 63
  • 98

1 Answers1

1

Is possible to achieve this type of querys?

Yes, it is possible.

When you are iterating through your queries MutableSet, at every iteration you are creating a new Query object. So you cannot simply call get() outside the loop only once and expect to have all those queries working. What can you do instead, is to add the get() call to every query inside the loop. The type of the object that results is Task<QuerySnapshot>. Add all those Task objects to a List<Task<QuerySnapshot>>. In the end, pass that list of tasks to Tasks's whenAllSuccess(Collection> tasks) method as explained in my answer from the following post:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thanks it worked perfect. My apologies to not accept before, I run out of office toooo fast. – Shudy Aug 15 '19 at 17:34
  • Hi again, just another question, related to this. @AlexMamo , it is possible instead of query for documents , add an event listener when some document changes /is added/deleted following this type of restrictions? Thanks in advance! – Shudy Aug 16 '19 at 12:33
  • Sure, check [this](https://firebase.google.com/docs/firestore/query-data/listen) out. You can listen for realtime updates. – Alex Mamo Aug 16 '19 at 15:06