Out of the Wild
- 10/17/2011
The Geous.js geolocation library exposes simple interfaces for locating users and performing geo-related calculations. Combined, these utilities can be used to create more sophisticated navigational tools. Here’s what it looks like:
<script src="https://raw.github.com/rjz/geous.js/master/src/geous.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>
function compass(destination) {
var me = geous.Me,
heading,
distance;
heading = geous.compute.compassHeading(me, destination);
distance = geous.compute.distanceBetween(me, destination);
return Math.round(distance*10)/10 + 'km ' + heading;
}
function init() {
var beervana = new google.maps.LatLng(45.521744, -122.695313);
alert( 'Beervana is ' + compass(beervana));
}
geous.withMe( init );
</script>
Get that? First, we defined a compass
function that uses Geous to compute the distance and heading between us and our destination—Portland, aka the Rose City, aka Beervana. This process takes advantage of two built-in functions:
geous.compute.compass
returns a heading (e.g., ‘NW’ or ‘SSW’) in string formgeous.compute.distanceBetween
returns the distance between two points in kilometers
We’ve kept it pretty simple here, but we could also use geous.compute.initialBearing
to tie a more precise heading into the compass’s readout. We could even convert the distance (currently in km) to local miles if we needed to–but Portland’s a pretty hip place, so we won’t bother.
Next, we created an init
function that will be called as soon as Geous is ready to go (that’s the withMe
part in the last line). init
calls compass
, and the rest, as they say, will speak for itself.
You can see a Geous compass in action at the Portland Beer Compass.