geoSearch method

List<Map<String, dynamic>> geoSearch(
  1. String field,
  2. double lat,
  3. double lng,
  4. double radius,
)

Implementation

List<Map<String, dynamic>> geoSearch(
    String field, double lat, double lng, double radius) {
  final List<Map<String, dynamic>> result = [];
  const double earthRadius = 6371; // in km
  for (final document in collection.values) {
    final dynamic value = document[field];
    if (value != null && value['lat'] != null && value['lng'] != null) {
      final double dLat = (value['lat'] - lat) * pi / 180;
      final double dLng = (value['lng'] - lng) * pi / 180;
      final double a = sin(dLat / 2) * sin(dLat / 2) +
          cos(lat * pi / 180) *
              cos(value['lat'] * pi / 180) *
              sin(dLng / 2) *
              sin(dLng / 2);
      final double c = 2 * atan2(sqrt(a), sqrt(1 - a));
      final double distance = earthRadius * c;
      if (distance <= radius) {
        result.add(document);
      }
    }
  }
  return result;
  // return _parseTimeStamp(result);
}