Databases 12 min read

Understanding Geohash: Principles, Implementation, and Applications

Geohash encodes latitude‑longitude pairs into short base‑32 strings by recursively bisecting coordinate ranges and interleaving bits, allowing fast proximity queries via prefix matching, with precision controlled by string length, and is supported natively in Redis and useful for location‑based services.

Tencent Cloud Developer
Tencent Cloud Developer
Tencent Cloud Developer
Understanding Geohash: Principles, Implementation, and Applications

Geohash is an algorithm that converts latitude and longitude into a short hash string, enabling fast and precise location queries in applications such as ride‑hailing and bike‑sharing.

Typical naive approaches store raw coordinates and scan them, which is inefficient. Geohash encodes a point into a base‑32 string; points with the same prefix are geographically close, allowing simple LIKE 'wtw366%' or LEFT(geohash,6)='wtw366' queries.

The encoding process consists of:

Specify the latitude and longitude.

Iteratively bisect the latitude (‑90 to 90) and longitude (‑180 to 180) ranges, recording 0 or 1 for each half‑interval.

Interleave the bits (even positions for longitude, odd for latitude).

Group the binary string into 5‑bit chunks and convert each chunk to a decimal value.

Map the decimal values to the base‑32 alphabet 0123456789bcdefghjkmnpqrstuvwxyz to obtain the final geohash.

Example PHP implementation (excerpt):

$minLat = -90; $maxLat = 90;
$minLng = -180; $maxLng = 180;
$latLength = $lngLength = 0;
$latList = $lngList = [];
$originPrecision = 30;
while ($latLength < $originPrecision) {
    $middle = ($minLat + $maxLat) / 2;
    if ($lat >= $middle) { $latList[] = 1; $minLat = $middle; }
    else { $latList[] = 0; $maxLat = $middle; }
    $latLength++;
}
while ($lngLength < $originPrecision) {
    $middle = ($minLng + $maxLng) / 2;
    if ($lng >= $middle) { $lngList[] = 1; $minLng = $middle; }
    else { $lngList[] = 0; $maxLng = $middle; }
    $lngLength++;
}

After interleaving and converting, the Shanghai Tencent Building (31.1688749, 121.3975184) yields the geohash wtw366ngz5qt . The length of the geohash determines precision: 12 characters correspond to ~37 mm, while 6 characters cover ~1.2 km.

Geohash has a boundary issue: two points near the edge of a cell may belong to different cells despite being closer than points inside the same cell. A common mitigation is to query the eight neighboring cells together.

Redis 3.2+ provides native GEO commands that store locations as 52‑bit integers (a form of geohash) in sorted sets, enabling fast radius queries via GEOSEARCH -like operations.

Distance between two coordinates can be computed with the spherical law of cosines or the haversine formula; PHP examples are provided.

SQLRedisphpprecisiongeohashspatial indexinglocation encoding
Tencent Cloud Developer
Written by

Tencent Cloud Developer

Official Tencent Cloud community account that brings together developers, shares practical tech insights, and fosters an influential tech exchange community.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.