Flutter Firestore Geo query
Asked Answered
A

2

9

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?

Arlynearlynne answered 19/6, 2019 at 19:17 Comment(4)
how were you able to solve the problem? I am facing dificulty integrating the same functionality ..Mishandle
I didn't. At the end of the day I give up on Firestore and I used ParseServer which has this functionality already implemented on it, and more than that is OpenSource. Also if you are interested I am using this one back4app.comArlynearlynne
Thank you for replying , but migrating from firebase is not an easy solution for me. I will try and implement the functionality somehow. and what about the bellow answer ?Mishandle
@Arlynearlynne Facing the same issue as you currently. Did back4app have any unforeseen drawbacks in the long run? Did it work well with Flutter for other backend functions, not only geolocation? Because completely switching from Firebase seems like a daunting task. Thank you in advance.Daugava
F
4

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.

Forgot answered 17/7, 2020 at 12:17 Comment(4)
Can you please explain what the lat and lon constants are? ThanksIrrecoverable
i have the same question. what the lat and lon constants are? if anyone have the answer, please comment.Velarize
Got it, The constant values of lat lon are the values of 1-mile distance from 0.0 latlongs from maps. when we multiply the distance by it. it will give the distance from our given location.Velarize
Hey dude, can you provide complete codes? ThanksAstrix
A
0

A bit late but I had the same problem and I decided to build an external server with distance ordering and limit functionality. Server stores objects that includes geopoint and Firestore document ID.

  1. The client sends a request to the server with center point and radius.
  2. The server returns a list of document IDs.
  3. Fetch documents from Firestore based on provided IDs.

The best I came up with.

Acceptor answered 18/2, 2021 at 16:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.