Sunil
Sunil

Reputation: 1

Firestore Geo Queries

Is there any way to query firestore db and return results order by distance (ascending/descending) from user's current location.

https://firebase.google.com/docs/firestore/solutions/geoqueries

This above documetation GeoQuery return documents with in the range but not in sorted order even after giving a direction.

    public Query getDocuments(MainActivity mActivity, GeoQueryBounds bounds) {
    return FirebaseFirestore.getInstance()
                            .collection("user")
                            .whereEqualTo("type", Integer.valueOf("1"))
                            .orderBy("geoHash", Query.Direction.ASCENDING)
                            .startAt(bounds.startHash)
                            .endAt(bounds.endHash)
                            .limit(10);

Upvotes: 0

Views: 499

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600036

The ways these libraries work means they by definition don't get the results from the database in order of distance. So if any library returns them in distance order, it's because they post-process the data.

If you use the approach in the documentation, you can post-process it in your application code. The code samples for querying geohashes has an example of using GeoFireUtils.getDistanceBetween() to determine the distance.

Upvotes: 1

Related Questions