Try it now: Geohash Chat by Proximity
To connect groups of two or more people by location, you will take lat/long values and reduce the resolution of accuracy, and by doing this you can expand the coverage of proximity. You can use multiple resolutions at once or a fixed resolution.
Get Source Code: GitHub Repository for Geo Chat by Proximity
Next we'll cover some source code snippet for geo hashing 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.
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Geo Hash
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
function geohash( coord, resolution ) {
var rez = Math.pow( 10, resolution || 0 );
return Math.floor(coord * rez) / rez;
}
"Zoom" levels, 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.
// Create Proximity Channel
channel = geohash( pos.latitude, 0 ) + '' + geohash( pos.longitude, 0 );
This will create a very wide circle and generate a channel name used to connect. Next connect to PubNub with this channel name.
// Connect to Proximity Channel
pubnub.subscribe({channels: [channel]});
pubnub.addListener({
message: function (m) {
printout(m.message);
}
});
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.
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.