Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Firestore Geo query

WHAT I HAVE TO DO:

I'm building an application where I want to do a query in firebase and receive the users who are in a range of X kilometers. But I want to limit this received documents and also to have a function for getting more documents on the scroll. I'm using a StreamBuilder->ListView.build in order to build my list. When I'm going to receive the end of the scrollable list I want to get more documents from Firestore (if I have any).


WHAT I'VE DONE ALREADY:

So far I found just 2 solutions for my problem:

  1. Firestore Helpers
  2. GeoFlutterFire

THE PROBLEM:

Both of those libraries are good with just one BIG problem, in both of this case I'm not able to limit the number of documents that I'm going to receive (I can JUST limit the original query for Firestore.instance.collection("MyColection")) but is not going to help me that much because I will have this case

Give me 10 documents from Firestore -> Give me the 5km range users -> I will receive just 4 let's say.

What I want instead is to receive 10 documents from Firestore within a range of X.

Is there any solution for this problem? Or I need to implement my own library for this? Or is not possible even so?

like image 272
Mircea Avatar asked Jun 19 '19 19:06

Mircea


People also ask

How do I retrieve data from firestore in Flutter?

How to get all data from a Firestore collection in flutter? Call the getDocs() function, then use the build function, then print all the document IDs in the console.

Does GeoFire work with firestore?

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.

How do I store my geolocation on firestore?

Saving Geolocation Data in FirestoreYou name the object whatever you want and save multiple points on a single document. The library provides a the point method to help you create this data. If updating an existing doc, you can use setPoint(id, lat, lng) to non-destructively update the document.

What is GeoFlutterFire?

GeoFlutterFire is an open-source library that allows you to store and query a set of keys based on their geographic location. At its heart, GeoFlutterFire simply stores locations with string keys.


1 Answers

GeoPoint userGeoPoint = user.geoPoint["geopoint"];

// distance in miles double distance = 30;

double lat = 0.0144927536231884;
double lon = 0.0181818181818182;
double lowerLat = userGeoPoint.latitude - (lat * distance);
double lowerLon = userGeoPoint.longitude - (lon * distance);

double greaterLat = userGeoPint.latitude + (lat * distance);
double greaterLon = userGeoPint.longitude + (lon * distance);

GeoPoint lesserGeopoint = GeoPoint(lowerLat, lowerLon);
GeoPoint greaterGeopoint = GeoPoint(greaterLat, greaterLon);
Query query = Firestore.instance
    .collection(path)
    .where("geoPoint.geopoint", isGreaterThan: lesserGeopoint)
    .where("geoPoint.geopoint", isLessThan: greaterGeopoint)
    .limit(limit);

This worked for me. Hope it helps.

like image 64
Julian Cassimiro Avatar answered Oct 19 '22 06:10

Julian Cassimiro