createGeohashes function

List<String> createGeohashes(
  1. double latitude,
  2. double longitude,
  3. double radius,
  4. int precision,
)

Implementation

List<String> createGeohashes(
    double latitude, double longitude, double radius, int precision) {
  if (precision > 12 || precision < 0) {
    throw ArgumentError('Incorrect precision found');
  }

  double x = 0.0;
  double y = 0.0;

  HashSet<String> geohashes = HashSet();

  double height = (gridHeight[precision - 1]) / 2;
  double width = (gridWidth[precision - 1]) / 2;

  int latMoves = (radius / height).ceil();
  int lonMoves = (radius / width).ceil();

  for (var i = 0; i < latMoves; i++) {
    double tempLat = y + height * i;
    for (var j = 0; j < lonMoves; j++) {
      double tempLong = x + width * j;

      if (inCircleCheck(tempLat, tempLong, y, x, radius)) {
        List<double?> centerList =
            getCentroid(tempLat, tempLong, height, width);
        double centerX = centerList[0]!;
        double centerY = centerList[1]!;

        geohashes.addAll([
          convertToGeohash(centerY, centerX, latitude, longitude, precision),
          convertToGeohash(-centerY, centerX, latitude, longitude, precision),
          convertToGeohash(centerY, -centerX, latitude, longitude, precision),
          convertToGeohash(-centerY, -centerX, latitude, longitude, precision),
        ]);
      }
    }
  }
  return geohashes.toList();
}