Is there a way to use GeoFire with Firestore?

FirebaseFirebase Realtime-DatabaseGoogle Cloud-FirestoreGeofire

Firebase Problem Overview


GeoFire is tightly coupled to the Realtime Database, while geo-queries are a common functional dependency of many apps that are looking to migrate to Firestore. Is there any way to replicate the hashing/retrieval of locations in the Firestore environment?

Firebase Solutions


Solution 1 - Firebase

Edit (Dec 17th, 2020): we have recently released a set of geo utility libraries and a guide to explain how to use them to implement simple geo queries on Firestore!

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

While we still don't have native geo queries in the database, these Android, iOS, and Web libraries will help you use Geohashes to get geo querying functionality.


Edit (July 1st, 2019): When I originally wrote the answer below I was optimistic that native geo queries would come to Cloud Firestore soon, which clearly did not happen. It's still in the long-term plans, but for now the best option is to use a community-built library or make your own using either GeoHashes or the S2 Geometry library.

The GeoFire libraries for Realtime Database were built using GeoHashes and porting the logic of those libraries to Cloud Firestore should be relatively simple.


Sam from the Cloud Firestore team here. As SUPERCILEX said, Cloud Firestore has support for the GeoPoint data type already. We are working hard to bring native geo queries to the product.

Because native geo queries are coming, we will not be porting GeoFire to Cloud Firestore. Instead we will redirect that engineering effort to getting the native queries sooner.

If you need geo queries today and don't want to build your own library, stick with Realtime Database!

Solution 2 - Firebase

GREAT NEWS. There is now a library for both iOS and Android that replicates GeoFire for Firestore. The library is called GeoFirestore. It has full documentation and is well tested. I currently use it in my app and it works brilliantly. The code is very similar to that of GeoFire so it should only take a few minutes to learn.

Solution 3 - Firebase

A solution that comes to mind is to add on the Realtime Database just for geo-queries and synchronize the two databases with Cloud Functions, much like Google suggests with presence.

In my case it's not even necessary to synchronize much; I just keep a list of uids with their locations on the Realtime Database and do all geo-queries there.

Solution 4 - Firebase

A new project has been introduced since the original poster first ask this question. The project is called GEOFirestore.

With this library you can perform queries like query documents within a circle:

  const geoQuery = geoFirestore.query({
    center: new firebase.firestore.GeoPoint(10.38, 2.41),
    radius: 10.5
  });

You can install GeoFirestore via npm. You will have to install Firebase separately (because it is a peer dependency to GeoFirestore):

$ npm install geofirestore firebase --save

Solution 5 - Firebase

The Javascript solution for GeoQuery with Firestore is to use GeoFirestore as Nikhil Sridhar said. But is a quite difficult to use (or it was for me).

First of all you have to initialize a GeoFirestore reference.

var firebase = require('firebase-admin'); 
var GeoFirestore = require('geofirestore');
// Create a Firestore reference
const firestore = firebase.firestore();
// Create a GeoFirestore reference
const geofirestore = new GeoFirestore.GeoFirestore(firestore);
// Create a collection reference but using geofirestore collections
// this is where you save the geofirestore documents with its structure
const geocollection = geofirestore.collection('userPositions');

After you have your collection initialized, the first step is save a document with the specified structure

{
   g: string;
   l: GeoPoint;
   d: DocumentData;
}

geofirestore.collection('userPositions').doc(id).set({ coordinates: new firebase.firestore.GeoPoint(0, 0)}).then(res => {
    return res;
  }).catch(err => {
    console.log(err);
  });

Only when you have your collection with geofirestore documents, you can query about them as the example said.

// Create a GeoQuery based on a location
  const query = geocollection.near({ center: new firebase.firestore.GeoPoint(0, 0), radius: 1000 });

  // Get query (as Promise)
  query.get().then((value) => {
    console.log(value.docs); // All docs returned by GeoQuery
  });

Hope this steps help you!

Solution 6 - Firebase

Cloud Firestore natively supports geo points. See the supported data types. And you'll find the GeoPoint class which you can use to set data and also query it:

query.whereEqualTo("location", GeoPoint(lat, long))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionurgentxView Question on Stackoverflow
Solution 1 - FirebaseSam SternView Answer on Stackoverflow
Solution 2 - FirebaseNikhil SridharView Answer on Stackoverflow
Solution 3 - FirebaseurgentxView Answer on Stackoverflow
Solution 4 - Firebasera9rView Answer on Stackoverflow
Solution 5 - FirebaseMati CassanelliView Answer on Stackoverflow
Solution 6 - FirebaseSUPERCILEXView Answer on Stackoverflow