简体   繁体   中英

Firestore: Query geopoints using bounds (lessThan/moreThan)

I want to query a collection of documents by their geopoints, using a bounding box taken from a map. I have the bounds:

NORTH: 50.730

EAST: -1.861

SOUTH: 50.700

WEST: -1.893

And this is my actual document structure (based on it's values I would expect it to return):

document: {
  d: {
    coordinates: GeoPoint [50.72, -1.87]
  }
}

This is what I'm trying, but it's not working:

db
  .collection('exploreMap')
  .where('d.coordinates.latitude', '<=', NORTH)
  .where('d.coordinates.latitude', '>=', SOUTH)
  // .where('d.coordinates.longitude', '<=', EAST)
  // .where('d.coordinates.longitude', '>=', WEST)
  .limit(80)
  .get()

It just returns an empty query, no errors. I have tried both the EAST/WEST query and NORTH/SOUTH, both the same result.

I know I can't do a full box query as Firestore doesn't support .where() on multiple fields, so doing a simple Latitude or Longitude query would be fine. I can then manually filter when the results are returned.

Can someone please advise on what I'm doing wrong?

I haven't found a way to query just longitude or latitude. But if you create GeoPoint with the correct latitude it works for me.

This returns all documents from my collection that are between latitude of 35 and 40:

var NORTH = new firebase.firestore.GeoPoint(40, 0);
var SOUTH = new firebase.firestore.GeoPoint(35, 0);

db
  .collection('10k')
  .where('location', '<=', NORTH)
  .where('location', '>=', SOUTH)

Working sample: https://jsbin.com/yatisun/edit?js,console

Since Firestore orders geopoints on latitude and then longitude, you can't filter on just longitude. You can only filter on longitude, once know the exact value of the latitudes you want to return (so not a range).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM