Turn a place on the map into a short text code, so nearby places share the same start.
A geohash turns a place on the map (latitude and longitude) into a short piece of text, like 9q8yyk. The longer the text, the smaller and more exact the area.
A place on Earth needs two numbers: latitude (north or south) and longitude (east or west). Databases are good at searching one number, such as a sorted column. They are not good at searching two numbers at the same time. Geohash solves this by squeezing the two numbers into one string.
Here is how it works. Start with the whole world as one box. Ask: is the place in the west half or the east half? Write down 0 or 1. Then ask: north half or south half? Write down 0 or 1. Keep taking turns (longitude, latitude, longitude, ...). After every 5 answers, turn the 5 bits into one character from a 32-letter alphabet (0-9 and b-z, without a, i, l, o). That text is the geohash.
The result is a grid of rectangles. Every rectangle has its own code, and a longer code is a smaller rectangle inside the shorter one. So 9q8y contains 9q8yy, 9q8yz and so on. This prefix rule is what makes geohash useful for search.
Each extra character makes the cell about 32 times smaller. These sizes are approximate and measured at the equator. Cells get narrower towards the poles.
| Characters | Cell size (about) | Good for |
|---|---|---|
| 1 | 5,000 km Γ 5,000 km | A huge part of the world |
| 3 | 156 km Γ 156 km | A region |
| 4 | 39 km Γ 19.5 km | A big city |
| 5 | 4.9 km Γ 4.9 km | A district |
| 6 | 1.2 km Γ 0.6 km | A few streets |
| 7 | 153 m Γ 153 m | A city block |
| 8 | 38 m Γ 19 m | A building |
| 9 | 4.8 m Γ 4.8 m | A room or a parking spot |
| Idea | What it means |
|---|---|
| Z-order curve | Taking turns between longitude and latitude bits walks the map in a zig-zag order. Nearby cells are often next to each other in that order, but not always. |
| Neighbour cells | A cell has 8 neighbours. Searching all 9 cells fixes the edge problem for small search circles. |
| Variable precision | You choose the length. Too short returns too many places, too long returns too few. |
| Shape of cells | Cells are rectangles that are narrower near the poles, so a cell is not a perfect circle of distance. |
Cyan = you, green = returned by the search, dashed amber ring = the truly nearest place, purple = neighbour cells.
Real geohash encoding (base 32, longitude bit first). Coordinates are approximate and distances are straight-line estimates. Each extra character adds 5 bits, so cells become 32 times smaller.
| Term | Meaning in easy words |
|---|---|
| Latitude / longitude | The two numbers that give a place on Earth: north-south and east-west |
| Geohash | A short string that names a rectangular cell on the map |
| Precision | How many characters the hash has. More characters means a smaller cell |
| Prefix | The start of a string. A hash that starts with another hash lies inside it |
| Base 32 | The 32-symbol alphabet geohash uses (0-9 and b-z without a, i, l, o) |
| Cell | One rectangle of the grid, named by one geohash |
| Neighbours | The 8 cells touching a given cell |
| Edge problem | Close places in different cells have different hashes |
| Z-order curve | The zig-zag order made by taking turns between the two coordinates |
| Haversine | A formula for the distance between two points on a sphere |
| Bounding box | A rectangle on the map, used to limit a search |
| Choice | We gain | We lose |
|---|---|---|
| Longer hash | A more exact area and fewer places to filter | More cells to search for a large radius |
| Shorter hash | Fewer cells to look up | Many more places to filter by distance |
| Search 9 cells | No missed neighbours across edges | More lookups and extra rows to throw away |
| Search 1 cell | Fastest, simplest query | Misses close places on the other side of an edge |
| Geohash | Simple and works everywhere | Uneven cells and the edge problem |
| S2 or H3 cells | More even cells and better neighbour handling | A special library and a more complex idea |