Skip to content

new function to retrieve documents #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 64 additions & 4 deletions lib/src/collection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,64 @@ class GeoFireCollectionRef {
}
}

/// Query firestore documents based on geographic [radius] in kilometers from geoFirePoint [center]
/// [field] specifies the name of the key that contains "geohash" and "geopoint" keys in the document
/// returns the list of [DocumentSnapshot]
///
/// Use [strictMode] parameter to filter by distance from center
Future<List<DocumentSnapshot>> getDocumentsWithin({
required GeoFirePoint center,
required double radius,
required String field,
bool strictMode = false,
}) async {
final precision = Util.setPrecision(radius);
final centerHash = center.hash.substring(0, precision);
final area = Set<String>.from(
GeoFirePoint.neighborsOf(hash: centerHash)..add(centerHash),
).toList();
print(area);
List<DistanceDocSnapshot> distanceDocSnapshots = [];
for (var hash in area) {
final tempQuery = _queryPoint(hash, field);
for (var querySnapshot in (await tempQuery.get()).docs) {
var distanceDocSnapshot = DistanceDocSnapshot(querySnapshot, null);
final fieldList = field.split('.');
Map<dynamic, dynamic> snapData =
distanceDocSnapshot.documentSnapshot.exists
? distanceDocSnapshot.documentSnapshot.data() as Map
: Map();
print(snapData);
var geoPointField = snapData[fieldList[0]];
//distanceDocSnapshot.documentSnapshot.data()![fieldList[0]];
if (fieldList.length > 1) {
for (int i = 1; i < fieldList.length; i++) {
geoPointField = geoPointField[fieldList[i]];
}
}
final GeoPoint geoPoint = geoPointField['geopoint'];
distanceDocSnapshot.distance =
center.distance(lat: geoPoint.latitude, lng: geoPoint.longitude);
distanceDocSnapshots.add(distanceDocSnapshot);
}
}

final filteredList = strictMode
? distanceDocSnapshots
.where((DistanceDocSnapshot doc) =>
doc.distance! <= radius * 1.02 // buffer for edge distances;
)
.toList()
: distanceDocSnapshots.toList();
filteredList.sort((a, b) {
final distA = a.distance!;
final distB = b.distance!;
final val = (distA * 1000).toInt() - (distB * 1000).toInt();
return val;
});
return filteredList.map((element) => element.documentSnapshot).toList();
}

/// Create combined stream listeners for each geo hash around (including) central point.
/// It creates 9 listeners and the hood.
///
Expand Down Expand Up @@ -146,10 +204,12 @@ class GeoFireCollectionRef {
return filtered;
}

/// Query firestore documents based on geographic [radius] from geoFirePoint [center]
/// [field] specifies the name of the key in the document
/// Query firestore documents based on geographic [radius] in kilometers from geoFirePoint [center]
/// [field] specifies the name of the key that contains "geohash" and "geopoint" keys in the document
/// returns merged stream as broadcast stream.
///
/// Use [strictMode] parameter to filter by distance from center
///
/// Returns original stream from the underlying rxdart implementation, which
/// could be safely cancelled without memory leaks. It's single stream subscription,
/// so only single listener could be created at a time.
Expand All @@ -170,8 +230,8 @@ class GeoFireCollectionRef {
);
}

/// Query firestore documents based on geographic [radius] from geoFirePoint [center]
/// [field] specifies the name of the key in the document
/// Query firestore documents based on geographic [radius] in kilometers from geoFirePoint [center]
/// [field] specifies the name of the key that contains "geohash" and "geopoint" keys in the document
/// returns merged stream as broadcast stream.
///
/// !WARNING! This causes memory leaks because under the hood rxdart StreamController
Expand Down