Ad

Query GeoHashes In Firetore In Range Android

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?

Ad

Answer

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:

Ad
source: stackoverflow.com
Ad