geohashQuery method

List<String> geohashQuery(
  1. String geoHash,
  2. int bits
)

Calculates the bounding box query for a geohash with x bits precision.

@param geohash The geohash whose bounding box query to generate. @param bits The number of bits of precision. @returns A start, end pair of geohashes.

Implementation

List<String> geohashQuery(String geoHash, int bits) {
  _validateGeoHash(geoHash);
  int precision = (bits / _BITS_PER_CHAR).ceil();
  if (geoHash.length < precision) {
    return [geoHash, geoHash + '~'];
  }
  geoHash = geoHash.substring(0, precision);
  String base = geoHash.substring(0, geoHash.length - 1);
  int lastValue = _BASE32.indexOf(geoHash[geoHash.length - 1]);
  int significantBits = bits - (base.length * _BITS_PER_CHAR);
  int unusedBits = (_BITS_PER_CHAR - significantBits);
  // delete unused bits
  var startValue = (lastValue >> unusedBits) << unusedBits;
  var endValue = startValue + (1 << unusedBits);
  if (endValue > 31) {
    return [base + _BASE32[startValue], base + '~'];
  } else {
    return [base + _BASE32[startValue], base + _BASE32[endValue]];
  }
}