1

I would like to know how to query geopoints in Firestore using Typescript. I have a collection named "locations" and documents in it with a geopoint field named "current_location". This last, has latitudes and longitudes stored in it.

I have been trying to access the field's "current_location" latitude and longitude in a query like this:

const querySnapshot = await admin.firestore()
       .collection('locations')
       .where('current_location.latitude' , '>=', lower_left_corner_latitude)
       .where('current_location.latitude' , '<=', upper_right_corner_latitude)
       .where('current_location.longitude', '>=', lower_left_corner_longitude)
       .where('current_location.longitude', '<=', upper_right_corner_longitude)
       .get() 

However, I am getting this error:

Error: 3 INVALID_ARGUMENT: Cannot have inequality filters on multiple properties: [current_location.latitude, current_location.longitude]

How can I access those properties in the query correctly?

Thanks,

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Milton
  • 891
  • 1
  • 13
  • 30
  • It seems that the latitude and longitude geopoint properties, cannot be accessed in a query. Therefore, you should create a new Geopoint variable based on the latitude and longitude. Then you can compare it to another Geopoint. In other words, the range filters compare geopoints to geopoints; not latitudes to latitudes, nor longitudes to longitudes. – Milton Aug 25 '18 at 08:40
  • 1
    There is no efficient way to directly query geopoints in Firestore at the moment. As you've found: performing range queries on lat and lon is not possible. The common approach is to add a geohash, and query on that. I gave a talk about this a while ago: https://www.youtube.com/watch?v=mx1mMdHBi5Q. Also see https://stackoverflow.com/questions/46553682/is-there-a-way-to-use-geofire-with-firestore – Frank van Puffelen Aug 25 '18 at 14:11

0 Answers0