encode method
Encodes a given Longitude and Latitude into a String geohash
Implementation
String encode(double longitude, double latitude, {int precision = 12}) {
bool precisionOdd = precision % 2 == 1;
int originalPrecision = precision + 0;
if (longitude < -180.0 || longitude > 180.0) {
throw RangeError.range(longitude, -180, 180, "Longitude");
}
if (latitude < -90.0 || latitude > 90.0) {
throw RangeError.range(latitude, -90, 90, "Latitude");
}
if (precision % 2 == 1) {
precision = precision + 1;
}
if (precision != 1) precision ~/= 2;
List<int> longitudeBits = _doubleToBits(
value: longitude, lower: -180.0, upper: 180.0, length: precision * 5);
List<int> latitudeBits = _doubleToBits(
value: latitude, lower: -90.0, upper: 90.0, length: precision * 5);
List<int> ret = [];
for (int i = 0; i < longitudeBits.length; i++) {
ret.add(longitudeBits[i]);
ret.add(latitudeBits[i]);
}
String geohashString = _bitsToGeoHash(ret);
return originalPrecision == 1
? geohashString.substring(0, 1)
: (precisionOdd
? geohashString.substring(0, geohashString.length - 1)
: geohashString);
}