Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query closest GeoPoints in a collection in Firebase Cloud Firestore?

The new Firestore DB allows me to store GeoPoints. Is there a way to query based on them?

So for example if each of my collections documents got a location field of the type geopoint. How can I get the closest 10 documents to an arbitrary coordinate?

The documentation doesn't seem to cover this. I tried stuff like this:

someRef.where('location', '>', geopoint).limit(10).get(); 

Obviously this doesn't make much sense but I'm just trying out some stuff here 😅

like image 552
ProblemsOfSumit Avatar asked Oct 06 '17 14:10

ProblemsOfSumit


People also ask

How do I get most recent documents on firestore?

Is there any way to get the last created document in Firebase Firestore collection? Yes, there is! The simplest way to achieve this is to add a date property to each object in your collection, then simply query it according to this new property descending and call limit(1) function. That's it!

What is GeoFirestore?

GeoFirestore is an open-source library for Android that allows you to store and query a set of documents based on their geographic location. At its heart, GeoFirestore simply stores locations with string keys.

Where is Databaseurl in Firebase?

You can find your Realtime Database URL in the Realtime Database section of the Firebase console. Depending on the location of the database, the database URL will be in one of the following forms: https:// DATABASE_NAME . firebaseio.com (for databases in us-central1 )


1 Answers

We haven't exposed geoqueries yet, so currently there isn't a way to do this.

Historically, the Firebase Realtime Database used geohashes in a library called GeoFire--but given that we plan to expose actual geoqueries in the near future, we haven't updated that library to Firestore. To learn how to do something similar yourself though, have a look at this video.

like image 128
Mike McDonald Avatar answered Sep 20 '22 07:09

Mike McDonald