Mozhgan Javadzadeh
Mozhgan Javadzadeh

Reputation: 139

Flutter Firestore Geo query for certain distance from my location

I stores my users data include their live location as GeoPoint data type in my firebase database. Now I need to select and get users from database with certain distance from my position. I use this line of code for retrieving my location:

position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);

No I need a firebase query in flutter to retrieve users which have 500 meters around me.It means I should retrieve all users with distance of 500 meters from firebase database. How can I do that?

Upvotes: 2

Views: 2088

Answers (1)

Mozhgan Javadzadeh
Mozhgan Javadzadeh

Reputation: 139

Finally I found solution:

position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
await getGenderIcons(context);
QuerySnapshot querySnapshot;
double lat = 0.0144927536231884;
double lon = 0.0181818181818182;
double distance = 1000*0.000621371;
double lowerLat = position.latitude - (lat * distance);
double lowerLon = position.longitude - (lon * distance);
double greaterLat = position.latitude + (lat * distance);
double greaterLon = position.longitude + (lon * distance);
GeoPoint lesserGeopoint = GeoPoint(lowerLat,lowerLon);
GeoPoint greaterGeopoint = GeoPoint(greaterLat,greaterLon);
querySnapshot = await usersRef
    .where("livelocation", isGreaterThan: lesserGeopoint)
    .where("livelocation", isLessThan: greaterGeopoint)
    .limit(100)
    .get();

Upvotes: 9

Related Questions