DarshanGowda0 / GeoFlutterFire

:fire:GeoFlutterFire:fire: is an open-source library that allows you to store and query firestore documents based on their geographic location.
https://youtu.be/MYHVyl-juUk
MIT License
305 stars 261 forks source link

Empty result when querying "within" on collection #191

Closed WieFel closed 2 years ago

WieFel commented 2 years ago

Hi,

I am always getting empty results when querying my collection for close documents within 1km. For being sure, i just created a new collection in my firestore called test, with one document which has some hardcoded coordinates. image

In my flutter code, i then query documents in a radius of 1km of that same pair of lat/lon, but I still get an empty result.

Stream<List<DocumentSnapshot>> get closeDocumentsStream {
    var radius = 1.0; // [km]
    var point = GeoFirePoint(36.723594, -4.4227609);
    return geo.collection(collectionRef: firebaseState.test).within(
          center: point,
          radius: radius,
          field: "position",
        );
  }

(where firebaseState.test is defined as get test => _firestore.collection("test");

Running Flutter 3.0.2 and Dart 2.17.3, with package versions geoflutterfire: ^3.0.2 and cloud_firestore: ^3.1.17.

Any hints on what I could be doing wrong?

bichoalexis commented 2 years ago

I found that is required to use geohash, try it!

WieFel commented 2 years ago

@bichoalexis can you give an example? I don't get what you mean exactly...

Just tried

.within(
        center: point.data,  // added .data
        radius: radius,
        field: "position",
      );

but it gives a type error, because point.data is no GeoFirePoint.

bichoalexis commented 2 years ago

I mean, in your Firestore database, when you create a new location, add geohash with geopoints, not just geopoints, then everything will work. By the way, if your are using the latest Firestore version with flutter web, I recommend you to change the dependency to geoflutterfire2. It has more support, this package looks abandoned. :)

WieFel commented 2 years ago

@bichoalexis thanks, I already use geoflutterfire2 now.

But still I don't get it to work. My document looks like this:

image
var radius = 100.0;
var point = geo.point(latitude: 0.0, longitude: 0.0);
return geo.collection(collectionRef: collection).within(
      center: point,
      radius: radius,
      field: "position",
    );

Whether I use position or position_hash as field, it doesn't work either...

bichoalexis commented 2 years ago

Try to write manually this in your latest document:

image
WieFel commented 2 years ago

@bichoalexis thank you so much, now I finally got it working! What I was missing was the correct data structure within firestore. immagine

geo.collection(collectionRef: collection).within(
      ...
      field: "point",
    );