getAtLocation method

Future<List<DocumentSnapshot<Map<String, dynamic>>>> getAtLocation(
  1. GeoPoint center,
  2. double radius,
  3. {bool exact = true,
  4. bool addDistance = true}
)

Returns the documents centered at a given location and with the given radius. center The center of the query radius The radius of the query, in kilometers. The maximum radius that is supported is about 8587km. If a radius bigger than this is passed we'll cap it. addDistance Whether to process data and add distance property to returned documents, defaults to True. exact Whether to process data and remove documents that are further than specified radius, defaults to True.

Implementation

Future<List<DocumentSnapshot<Map<String, dynamic>>>> getAtLocation(
  GeoPoint center,
  double radius, {
  bool exact = true,
  bool addDistance = true,
}) async {
  // Get the futures from Firebase Queries generated from GeoHashQueries
  final futures = GeoHashQuery.queriesAtLocation(
          center, GeoUtils.capRadius(radius) * 1000)
      .map((query) => query.createFirestoreQuery(this).get());

  // Await the completion of all the futures
  try {
    List<DocumentSnapshot<Map<String, dynamic>>> documents = [];
    final snapshots = await Future.wait(futures);
    snapshots.forEach((snapshot) {
      snapshot.docs.forEach((doc) {
        if (addDistance || exact) {
          final latLng = doc.data()['l'];
          final distance = GeoUtils.distance(center, GeoPoint(latLng[0], latLng[1]));
          if (exact) {
            if (distance <= radius) {
              doc.data()['distance'] = distance;
              documents.add(doc);
            }
          } else {
            doc.data()['distance'] = distance;
            documents.add(doc);
          }
        } else {
          documents.add(doc);
        }
      });
    });
    return documents;
  } catch (e) {
    print('Failed retrieving data for geo query: ' + e.toString());
    throw e;
  }
}