34

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

ProblemsOfSumit
  • 19,543
  • 9
  • 50
  • 61

5 Answers5

34

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Mike McDonald
  • 15,609
  • 2
  • 46
  • 49
  • 67
    Any idea how long before the geoqueries will be available? I am beginning development of an app that will rely on this functionality and I was about to use Realtime Database with GeoFire, but I would prefer to build the app with the new Firestore. – Eric Engel Oct 08 '17 at 01:19
  • 9
    Am also waiting for this, or at least a simple compound query on multiple fields. Was really disappointed to see `Uncaught Error: Invalid query. All where filters with an inequality (<, <=, >, or >=) must be on the same field.` when trying out a query on `lat` and `lng`... This seems like the most basic functionality you'd expect even from a database in beta. – Magnus Oct 31 '17 at 09:35
  • 11
    @Tequilaman is there a roadmap of upcoming features for FireStore? waiting on this one too and seriously considering implementing geohash search though if I can avoid it, i'd rather leave it to the experts :) – Teto Dec 01 '17 at 02:37
  • @Frank-van-Puffelen can you share what's actually happening with a query like the one above? I'm querying geopoint data using ranges, and they don't fail, so I'm curious what's actually happening. – Sam Storie Jan 28 '18 at 18:05
  • @Sam Storie what do you mean by ranges? Also, does anyone know what "near future" means? Thanks – philtz Mar 20 '18 at 23:06
  • I can't wait and I don' t want to pay Algolia (*it costs too much when not doing open source*). My solution is to use [this library called Geolib](https://github.com/manuelbieh/Geolib). Yes a bit more code by hand but it does the job without leaving Cloud Firestore solution. – MacKentoch Apr 01 '18 at 19:45
  • 3
    geoqueries will be available? – Francesco Apr 08 '18 at 21:11
  • 2
    Going to use geofirestore for now, but I'd like to know when this "near future" will be. Is there a roadmap somewhere? – arao6 Jul 10 '18 at 17:19
  • When will be official geo queries? – rendom Aug 16 '18 at 10:08
  • 5
    Jan 2019. is it near yet ? – Mohamed Nageh Jan 14 '19 at 20:36
  • @mohnage7 There's a library called Geofirestore (https://github.com/imperiumlabs/GeoFirestore) that works well. – DHShah01 Feb 21 '19 at 08:44
  • @DHShah01 yes i'v used it. Thank you. – Mohamed Nageh Feb 21 '19 at 21:42
  • 3
    You can query geopoints using this library. I tested it out and it works like a charm. Measures the distance in kilometers along with constraints. https://pub.dartlang.org/packages/geoflutterfire – mike-gallego May 01 '19 at 19:02
  • @DHShah01 I am also using geofirestore. It works fine. But the only problem i have with it is there is no any way to limit the query, so not able to do pagination. – Jayesh Babu Nov 04 '19 at 05:04
  • "in the near future"... it's been 32 months. Has anyone heard anything about this feature being released any time soon? – undefined Jun 03 '20 at 05:21
  • When it will be available? It's been a long time since 2019? @MikeMcDonald – Divyesh Jun 23 '20 at 06:30
  • 1
    Hey folks, I haven't worked at Google for about a year, so I'm not sure what the current plans are. I'd recommend taking a look at https://issuetracker.google.com/issues?q=firestore and filing an issue. – Mike McDonald Jun 25 '20 at 22:41
  • Unfortunately the issue is still ignored https://issuetracker.google.com/issues/160523401 – nibbana Dec 06 '20 at 18:51
8

A new project has been introduced since @problemsofsumit 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

You can link to it via cdn: https://cdn.jsdelivr.net/npm/geofirestore@2.2.2/dist/geofirestore.min.js

Binyamin Green
  • 83
  • 1
  • 2
  • 11
ra9r
  • 4,528
  • 4
  • 42
  • 52
  • can you elaborate on the installation for geoFirestore, im getting this error: https://stackoverflow.com/questions/51454724/error-installing-geofirestore-could-not-find-remote-branch-1-1-0-to-clone – David Henry Jul 22 '18 at 03:45
  • Het David, the geofirestore ra9r is referring to is different than the one in your comment. Mine is designed for JS, not iOS or Android (however I'd love to work with people who have ideas for those platforms as well!) – MichaelSolati Jul 27 '18 at 17:39
  • npm install geofirestore firebase --save is for firebase realtime database not firestore? – channae Sep 23 '18 at 15:20
3

You could let Algolia do the geo search with radius for you. All it would require is to build a cloud function that triggers on your wanted documents and sync them to Algolia.

Take a look at Geo Search https://www.algolia.com/doc/guides/searching/geo-search/

Also Firebase documentation advertises Algolia as a solution to complex searches here https://firebase.google.com/docs/firestore/solutions/search

Troels Lenda
  • 524
  • 2
  • 12
1

Rather than use third party services like Algolia I would use the new library that came out for both iOS and Android that replicates GeoFire for Firestore. The new library, called GeoFirestore, is fully documented and well tested. I have used this library already in many demo projects and it seems to work perfectly. Give it a shot!

Nikhil Sridhar
  • 1,670
  • 5
  • 20
  • 39
  • 1
    I'm getting this error when installing via. cocoa pods did you get this error? https://stackoverflow.com/questions/51454724/error-installing-geofirestore-could-not-find-remote-branch-1-1-0-to-clone – David Henry Jul 22 '18 at 03:39
  • Cocoapods recently got updated. Since Imperium Labs has created a new release, you must run "pod repo update" and then "pod install" as the instructions say – Nikhil Sridhar Jul 25 '18 at 04:32
  • @NikhilSridhar is there any way to paginate the query in GeoFirestore, or atleast limit the results? – Jayesh Babu Nov 04 '19 at 05:07
0

After scouting the net, i found this GeoFirestore library by chintan369 that you could use to query nearby geopoints. The queries are based on Firestore native queries. Just add below line into your build.gradle (project-level):

allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}

Then add the library to your build.gradle (app-level):

dependencies {
  implementation 'com.github.chintan369:Geo-FireStore-Query:1.1.0'
}

...and sync your project.

Visit https://medium.com/android-kotlin/geo-firestore-query-with-native-query-bfb0ac1adf0a for more.