bounds method

  1. @override
BoundsRecord bounds(
  1. int geohash
)

Returns a bounding box for a geohash as an annotated record.

Note that for the same geohash precision, box dimensions in degrees are all the same, regardless of latitude. Meaning, you can calculate dimensions of adjacent boxes without having to calculate their geohashes.

Implementation

@override
BoundsRecord bounds(int geohash) {
  if (geohash == 0) {
    throw ArgumentError('Geohash cannot be empty');
  }

  double minLat = -90;
  double maxLat = 90;
  double minLon = -180;
  double maxLon = 180;
  bool evenBit = true;
  int bit = 1 << (geohash.bitLength - 2);

  while (bit > 0) {
    if (evenBit) {
      final midLon = (minLon + maxLon) / 2;
      if (geohash & bit > 0) {
        minLon = midLon;
      } else {
        maxLon = midLon;
      }
    } else {
      final midLat = (minLat + maxLat) / 2;
      if (geohash & bit > 0) {
        minLat = midLat;
      } else {
        maxLat = midLat;
      }
    }

    bit >>= 1;
    evenBit = !evenBit;
  }

  return (minLat: minLat, minLon: minLon, maxLat: maxLat, maxLon: maxLon);
}