javaandroidfirebasegoogle-cloud-firestoregeopoints

Can I query a Firestore database by GeoPoint values to find local places?


I'm wondering if this is possible, as I've searched for quite a while and not found any documentation that shows that a query can be made against stored GeoPoints in Firestore from Android in Java?


Solution

  • Can I query a Firestore database by GeoPoint values to find local places?

    Sure you can. If you want to query Firestore by a specific GeoPoint object, then you should perform a Query. Assuming that you have in the database a document that has a property of type GeoPoint which holds, for example, 51.5074 for latitude and 0.1278 for longitude, then the following query is required:

    FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
    CollectionReference placesRef = rootRef.collection("places");
    Query geoQuery = placesRef.whereEqualTo("geoPoint", new GeoPoint(51.5074, 0.1278));
    geoQuery.get().addOnCompleteListener(/* ... */);
    

    However, if you want to query nearby locations, please note that Firebase recently launched Geo queries, meaning that you can get documents that would fall into a specific radius.