bounds method

  1. @override
BoundsRecord bounds(
  1. String 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(String geohash) {
  double minLat = -90;
  double maxLat = 90;
  double minLon = -180;
  double maxLon = 180;
  bool evenBit = true;

  if (geohash.isEmpty) {
    throw ArgumentError('Geohash cannot be empty');
  }

  for (final char in geohash.toLowerCase().codeUnits) {
    final idx = _kCodeUnitToInt[char];
    if (idx == null) {
      throw ArgumentError('Invalid characters in the geohash: "$geohash"');
    }

    for (int bit = 4; bit >= 0; bit--) {
      final v = (idx >> bit) & 1;
      if (evenBit) {
        final midLon = (minLon + maxLon) / 2;
        if (v == 1) {
          minLon = midLon;
        } else {
          maxLon = midLon;
        }
      } else {
        final midLat = (minLat + maxLat) / 2;
        if (v == 1) {
          minLat = midLat;
        } else {
          maxLat = midLat;
        }
      }
      evenBit = !evenBit;
    }
  }

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