This geohashing tutorial walks you through how to build a real-time chat app that connects users based on relative location (ie. geohashing).
To connect groups of two or more people by location, you will need to take lat/long values and reduce the resolution of accuracy. By doing this, you can expand the coverage of proximity. You can use multiple resolutions at once or a fixed resolution.
Want to see geohashing in action? A full working demo can be seen here: Geohashing Chat by Proximity. Or check out the source code here: GitHub Repository for Geohashing Chat by Proximity.
Basics of Geohashing
You’ll first need to sign up for a PubNub account. Once you sign up, you can get your unique PubNub keys in the PubNub Developer Portal. Once you have, clone the GitHub repository, and enter your unique PubNub keys on the PubNub initialization, for example:
Next, we’ll cover some source code snippet for geohashing lat/long coords. It is fairly simple to increase the radius of lat/long position by reducing the accuracy of the float values. Using this you can expand the circle and collect a Lat/Long.
Geohashing Resolution “Zoom”
“Zoom” levels are basically several different cartesian grids with larger and smaller granularity. Once a Geofencing event fires, you will publish to a set of channels corresponding with each zoom level. Zoom levels are important as that is how we actually construct the link between Geofencing and PubNub. Zoom level is the resolution/de-resolution of the Cartesian coordinates Lat/Long (think X,Y coord).
By reducing the resolution of the lat/long coord we can construct a channel name that hits 1 box of the grid. Less resolution means larger the boxes and is required to determine a PubNub Channel that is associated.
This will create a very wide circle and generate a channel name used to connect. Next connect to PubNub with this channel name.
Multiplexing 9 Boxes
You will calculate the surrounding squares to extend the taper and radius in a way that provides more accuracy. This will remove the “fencing” effect.
Geohashing with PubNub Conclusion
That’s it! You simply reduce the resolution of a geo coordinate and use that as a channel name on PubNub. Also check out the browser’s
navigator.geolocation.getCurrentPosition(...) method to acquire lat/long in a Chrome/Firefox/IE/Opera/mobile/Safari browser.
We have a real world, Internet of Things example in our Connected Car Solution Kit. With GeoFencing, trigger actions and alerts based on vehicle location and geographic boundaries (e.g. only vehicles within five miles get dispatch requests, customized traffic alerts, etc.).