How to Convert Latitude and Longitude into a String Using Geohash

I use Geohash all the time to store latitude and longitude inside a MySQL or SQLite database. If I'm using Postgres, Couch, or MongoDB, latitude, and longitude are first-class citizens, but I can't always use those databases.

Example

Here's a simple app example that theoretically publishes a geohash to a browser that then triggers Google Maps to plot.


<!DOCTYPE html>
<head>
  <title>Geo Hash Example</title>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script> 
    <script type="text/javascript">
    var loadMap = function(){
        var myLatlng = new google.maps.LatLng( <%= lat %>, <%= lon %>);
        var myOptions = {
            zoom: <%= zoom %>,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("map"), myOptions);
    };
    window.onload = loadMap; 
    </script>
</head>
<body>
    <h2>Geohash: <%= geohash %></h2>
    <div id="map" style="width:500px;height:500px;"></div>
</body>
</html>
/************************************************
* Title: Server
* Desc:
* Modified:
* Notes:
* GeoHash is an algorithm that interweaves lat,lng 
* into a string that offers a unique ID. Since datastores 
* don't have strong spatial indexing support, geohashes 
* are the perfect solution.
*
* Special handling of proximity queries for points on
* the edge of a Geohas bounding box can be compensated for
* by doing lookups & queries of the surrounding Geohash 
* bounding boxes.
* 
* Example
* http://universimmedia.pagesperso-orange.fr/geo/loc.htm
* http://boundingbox.klokantech.com/
* 
* //http://localhost:8080/34.03947,-118.26135
* //http://localhost:8080/9q5csuswqc45
* //http://geohash.org/?q=34.03947,-118.26135&format=url&redirect=0
************************************************/

var express = require("express");
var geohash = require("geohash").GeoHash;
var app     = express.createServer();
app.get("/:id", onIndexHandler );
app.listen( process.env['app_port'] || 8080 );

function onIndexHandler( req, res ){
  var hash = "";
    var params = req.params["id"];
    var arr = params.split(',');
    
    //If coords were passed in, conver them to a hash
    if( arr.length > 1 ){
        hash = geohash.encodeGeoHash( arr[0], arr[1] );
    }
    //If a hash was passed in, keep it as is 
    else {
        hash = params;
    }
    //Decode the hash
    var latlng    = geohash.decodeGeoHash( hash );
    var lat        = latlng.latitude[2];
    var lng        = latlng.longitude[2];            
    //In order to make use of the precision of the geohash, it can be used to control the inital zoom level of the map.
    var zoom = hash.length + 2;
    //console.log( lat, lng, zoom );
    res.render( "index.ejs", {
        layout:   false,
        lat:      lat,
        lon:      lng,
        zoom:     zoom,
        geohash:  hash
    });
}

My Github Source


Resources