Is there a way to use GeoFire with Firestore?
Asked Answered
C

6

60

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?

Carom answered 3/10, 2017 at 21:25 Comment(3)
any news about a native Firestore geo-query support? I will give a go to GeoFirestore anywayInspissate
Take a look at the accepted answerNakamura
Current status: Firebase native support in limbo. 3rd party GeoFirestore is implemented in (Swift) github.com/imperiumlabs/GeoFirestore and (JS) npmjs.com/package/geofirestore.Indistinctive
N
21

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.

Nakamura answered 10/7, 2018 at 6:19 Comment(3)
What about web(javascrip) ?? :(Ofay
@Ofay seems like there is javascript support There is much more documentation here geofirestore.com/#example-usageSears
Nikhil Sridhar is in the team of this productAnglicism
N
55

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!

Nagana answered 3/10, 2017 at 23:40 Comment(23)
Firestore will then be the place to go, how far are we from this implementation? A couple of day? Weeks? Years?Histamine
Yes, an approximate timeline would be extremely helpful. We are just now staring a large app with geodata and would be open to implement it with Firestore if we have Geofire like functionality soonish.Goldin
I can't share any timelines. It could be a while though! If this is a critical feature for your app please use existing tools rather than waiting for us to do this.Nagana
Hi @SamStern. Any news from geo queries? I used Firestore before checking if geo queries exist, so now I'm stuck. Thank you.Hyperventilation
Hi @ArdaOğulÜçpınar In case you are not aware, use firebase to store geo points via geofire. Keep a reference for those data in firestore. This could be a temporary solution until the availability of geo queries.Biblioclast
Hi @kalan, I've taken a look at GeoFire. It's querying geo info on the go I suppose, I mean on the device, not at the server-side. I've already implemented on-the-device querying for my specific needs, however, GeoFire might be faster than my implementation. A question. Will it be easy to remove GeoFire and use the native support of geo-querying when it's ready to use?Hyperventilation
@ArdaOğulÜçpınar I see. As I understand native support of geo-querying will introduce storing data in firestore. In that case we may store/ process new data directly from firestore using native geo-querying. However, We may need to think about transferring our existing firebase data to firestore for native geo-querying.Biblioclast
Any update regarding this topic? I would like to migrate from RTDB to Cloud Firestore but I need to perform geo queries in Swift (listening to updates like with GeoFire)Inspissate
April already. The wind whistles gently carrying my hopes with it. Any update...? :(Winne
Geo query support is still planned but it's a long-term plan. I don't expect to have an update very soon.Nagana
It already 6 months that should be long enough. What you mean long term? Is it 3 years? 6 years? 10 years? 100 years? Or the death of the sun and the last blackhole in the universe?Monongahela
Talked with the team on google IO 2018 and it seems this is long term and not near term. I would expect 1y+. We will start using algolia for free text search and also start using their geo search.Deutoplasm
@SimonBengtsson That's sad to hear. I was expecting it sooner. I decided to give Firestore a chance because this was supposed to come in the near term. Now I'm hesitating to look for an alternative.Winne
Wow, that's far away. Guess I'll just have to go back to Realtime Database.Swahili
Unfortunate... Guess I'll have to use both databases now, one for users and such and the other for geo-queries.Kantos
@Kantos not anymore! A new library called Geofirestore (github.com/imperiumlabs/GeoFirestore) has been released, available through CocoaPods for iOS and Gradle for Java.Epp
@SimonBengtsson Algolia isn't necessary if you only want the functionality of geo-based queries. See comment above ^ a new library has been released for Geofire support with Firestore and it works great!Epp
Great! I found another for those of us using NPM - github.com/MichaelSolati/geofirestoreKantos
@SamStern I was wondering whether you have an update regarding when approx. geolocation querying (i.e. find all objects within a certain range of a GeoPoint) will be supported by Firestore (don't worry I won't hold you to any date :)) ? If it is nearish (say within 3 months) I can wait for that, otherwise probably need to go the geohashing way. Thanks!!!Sewell
Good morning 2019, no geo queries stillLocality
@SamStern are you able to roughly explain how the native geoquery will work or look? For example, will it require performing a range operation? Any info would really help data modeling with the now generally-available Firestore.Epicarp
@bsod I suppose it is enough for now to add a GeoPoint to your documents. This is at least what I will do.Magree
New guide here showing how to use geo hashes! firebase.google.com/docs/firestore/solutions/geoqueriesNagana
N
21

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.

Nakamura answered 10/7, 2018 at 6:19 Comment(3)
What about web(javascrip) ?? :(Ofay
@Ofay seems like there is javascript support There is much more documentation here geofirestore.com/#example-usageSears
Nikhil Sridhar is in the team of this productAnglicism
C
11

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.

Carom answered 3/10, 2017 at 21:25 Comment(1)
I can't, unfortunately. All you need to do is delete UIDs in the Realtime DB when they are deleted in the main Firestore DB. For querying you just write your locations to Realtime DB and retrieve your list of UID's with GeoFire.Carom
D
7

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
Derek answered 27/6, 2018 at 2:30 Comment(3)
This is practically useless since it only works in JavascriptNakamura
@NikhilSridhar As a javascript developer, I disagree :). FWIW, looks like someone might be working on a mobile version: github.com/imperiumlabs/GeofirestoreShaffer
I have a users collection in firestore, doc ref of each is the userId. Within each user document there is a location stored in GeoPoint. Do I need to alter my firestore structure to use GeoFirestore???Olcott
F
-1

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!

Frosting answered 13/2, 2019 at 18:28 Comment(0)
G
-2

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))
Garald answered 3/10, 2017 at 22:6 Comment(3)
Hello SUPERCILEX, does this work with a range ? (Query a city and get all restaurant in that city for example) not a specific oneThiamine
But what about range based queries on a field like 'location'? Those queries don't fail, but it's not clear what they're actually doing...Wineskin
This completely misses the point of the question. Without the queries, the data type is effectively useless.Derek

© 2022 - 2024 — McMap. All rights reserved.