createGeohashesBoundingBox function

List<String> createGeohashesBoundingBox(
  1. double minlatitude,
  2. double minlongitude,
  3. double maxlatitude,
  4. double maxlongitude,
  5. int precision,
)

Implementation

List<String> createGeohashesBoundingBox(double minlatitude, double minlongitude,
    double maxlatitude, double maxlongitude, 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;

  double centerLatitude = (maxlatitude + minlatitude).abs() / 2;
  double centerLongitude = (maxlongitude + minlongitude).abs() / 2;

  int latMoves = ((maxlatitude - minlatitude).abs() / height).ceil();
  int lonMoves = ((maxlongitude - minlongitude).abs() / 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;

      List<double?> centerList = getCentroid(tempLat, tempLong, height, width);
      double centerX = centerList[0]!;
      double centerY = centerList[1]!;

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