Skip to content

Instantly share code, notes, and snippets.

@girliemac
Last active October 14, 2017 03:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save girliemac/47b2857feafafcc5cff4de76bfbe9b7e to your computer and use it in GitHub Desktop.
Save girliemac/47b2857feafafcc5cff4de76bfbe9b7e to your computer and use it in GitHub Desktop.
Geohashing
var lat, lon;
if ('geolocation' in navigator) {
navigator.geolocation.getCurrentPosition(function(position) {
lat = position.coords.latitude;
lon = position.coords.longitude;
console.log('lat: ' + lat);
console.log('lon: ' + lon);
console.log('geohash: ' + geohash(lat, 0) + '' + geohash(lon, 0));
});
}
function geohash(coord, resolution) {
var rez = Math.pow(10, resolution || 0);
return Math.floor(coord * rez) / rez;
}

An example of how you can define geohash grids for users' locations

At resolution 0, each grid size is approx 111km x 111km (circumference of the earth 40,075km/360deg). Resolution 1 gives 11km x 11km grids.

The example below is for SoMa district in San Francisco at lat: 37.7834462, lon: -122.39952480000001 (Geohash grid is 37-123).

geohash example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment