GeoHashExtensions { public static String encode(final double latitude, final double longitude) { final StringBuilder geohash = new StringBuilder(); boolean even = true; int bit = 0; int ch = 0; final double[] latitudeInterval = { -90.0, 90.0 }; final double[] longitudeInterval = { -180.0, 180.0 }; while (geohash.length() < precision) { double mid = 0.0; if (even) { mid = (longitudeInterval[0] + longitudeInterval[1]) / 2D; if (longitude > mid) { ch |= bits[bit]; longitudeInterval[0] = mid; } else { longitudeInterval[1] = mid; } } else { mid = (latitudeInterval[0] + latitudeInterval[1]) / 2D; if (latitude > mid) { ch |= bits[bit]; latitudeInterval[0] = mid; } else { latitudeInterval[1] = mid; } } even = !even; if (bit < 4) { bit++; } else { geohash.append(BASE_32[ch]); bit = 0; ch = 0; } } return geohash.toString(); }  static double[] decode(final String geohash); static double[] decodeAndRound(final String geohash); static String encode(final double latitude, final double longitude); static String getAdjacent(final String geohash, final String direction); static String getAdjacent(final String geohash, final String... directions); static List<String> getAllAdjacentAreasList(final String geohash); static Map<String, String> getAllAdjacentAreasMap(final String geohash); static FirstAndSecondRingRegion getFirstAndSecondRingRegion(final String geohash); static FirstRingRegion getFirstRingRegion(final String geohash); static double getLatitude(final String geohash); static double getLongitude(final String geohash); static Map<String, String> getTwentyFiveAreasMap(final String geohash);  }
@Test public void testEncode() { String expected; String actual; actual = GeoHashExtensions.encode(53.5526394, 10.0067103); expected = "u1x0esywtr81"; assertEquals(expected, actual); actual = GeoHashExtensions.encode(30.0, -90.0); expected = "9vrfxvrfxvrf"; assertEquals(expected, actual); actual = GeoHashExtensions.encode(51.4797, -0.0124); expected = "gcpuzewfzp9u"; assertEquals(expected, actual); }