Geohashing

What is geohashing exactly?

Will Hill
4 min readApr 22, 2017

Geohash is a geocoding system invented by Gustavo Niemeyer that allows us to know what area on a map a user is within. The user could be anywhere in that area, they are not necessarily in the center of the area.

This is really useful as latitude and longitude only represents a single point on a map where as a geohash describes a fixed area.

We can change the size of the area by specifying the number of characters in the hash from 1 to 10. Below from left to right we can see the geohash area for 1,2 and 3 character hashes.

It’s worth noting that in the above example they have all start with a “9” and the second character is a “q” and the third is an “8” .

Geohashing can also be used to provide a degree of anonymity as we dont need to expose the exact location of the user, depending on the length of the geohash we just know they are somewhere within an area.

We can also use this to know when 2 users are close to each other, let’s consider these 2 users shown below, who are each represented by a 9 digit geohash. Nine digits is very accurate it lets us know they are somewhere within a 16ft square (4.7 meters)

These 2 users are both in the “9q8” area, one is in “9q8y” and one is in “9q8z”

We can see the size of each of the Geohash lengths below. and an interactive map showing the areas is here

How we figure out which geohash a user is within, starts with knowing the user's latitude and longitude. When we ask the user for their location, the user needs to approve the request before we get their location.

We can ask the user for permission at any point, the most success comes from letting the user know or making it very clear that we are about to request permission or we going to need permission once the user performs an action like clicking a button.

A simple function to get the users latitude and longitude looks like..

The rest of the information about how the hash is generated we could continue to research, or we could save time and trust that someone else has figured this out already and published a package that we can simply use, knowing that a community of people have audited the code, found and fixed bugs.

Including the above allows us to simply use

Geohash.encode(lat, lon, [precision])

Asking a user for their location as soon as the home page loads is penalised by Lighthouse. Lighthouse is an open-source, automated tool for improving the quality of web pages from google. Getting a 90+ percentage score is rapidly becoming a modern standard for all websites.

Responsible use

Asking for and obtaining a user's geographical position comes with some responsibility. Geohashing used with appropriate length provides the user some protection, but it us down to us as developers to only broadcast the length required and not the users exact location.

Adding somthing like the below to your terms and conditions would be advised.

User privacy is extremely important to us at <insert your name>. Location coordinates are only gathered after the user has agreed to use this service.

If age restricted content applies, you need to state that user must be 18+ years of age to access the service.

Location coordinates (longitude, latitude, and radius) will be obtained ONLY when opt-in conditions are met and users are automatically opted- out after an attempt to locate them has been completed. Precise location data is not stored, geo hashing to level 4 which places them somewhere within a 39km x 19km area is stored and transmitted to other users.

--

--