Geolocation with navigator.geolocation
The W3C have been working on a Geolocation API Specification to incorporate geolocation into the browser via a new variable, navigator.geolocation.
navigator.geolocation can provide us with the following details:
- latitude
- longitude
- altitude
- accuracy
- altitudeAccuracy
- heading
- speed
Mozilla Firefox v3.5 introduced navigator.geolocation, and so far Mozilla Firefox has been the only browser implementing it.
Thanks to Mozilla Firefox v3.5 we now have Location-Aware Browsing! Click here for an example. A bar will drop from the top prompting you that "blog.aizatto.com wants to know your location" with options on the right hand side. Click on "Share location".
Making Your Application Location Aware
I am going to use my sample HTML file in my previous article Geolocation with Google AJAX API (uploaded file).
This allows us to reuse the Google Reverse Geocoding.
It has been reproduced here for easy reference:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>Geolocation with Google AJAX API</title>
</head>
<script src="http://www.google.com/jsapi?key=ABCDEF" type="text/javascript"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script language="Javascript" type="text/javascript">
//<![CDATA[
function OnLoad() {
var geocoder = new google.maps.Geocoder();
if (google.loader.ClientLocation) {
document.body.innerHTML += 'google.loader.ClientLocation.latitude ' + google.loader.ClientLocation.latitude + '<br />';
document.body.innerHTML += 'google.loader.ClientLocation.longitude ' + google.loader.ClientLocation.longitude + '<br />';
document.body.innerHTML += 'google.loader.ClientLocation.address.city ' + google.loader.ClientLocation.address.city + '<br />';
document.body.innerHTML += 'google.loader.ClientLocation.address.country ' + google.loader.ClientLocation.address.country + '<br />';
document.body.innerHTML += 'google.loader.ClientLocation.address.country_code ' + google.loader.ClientLocation.address.country_code + '<br />';
document.body.innerHTML += 'google.loader.ClientLocation.address.region ' + google.loader.ClientLocation.address.region + '<br />';
document.body.innerHTML += '<a href="http://maps.google.com/maps?q=' + google.loader.ClientLocation.latitude + ',+' + google.loader.ClientLocation.longitude + '>Open Location in Google Maps</a><br />';
geocoder.geocode({'latLng': new google.maps.LatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude)}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
document.body.innerHTML += 'google.maps.Geocoder ' + results[0].formatted_address;
}
} else {
document.body.innerHTML += 'Geocoder failed due to: ' + status;
}
});
} else {
document.body.innerHTML += 'Cannot find location';
}
}
google.setOnLoadCallback(OnLoad);
//]]>
</script>
<body>
</body>
</html>
Using navigator.geolocation
Its time to replace OnLoad again:
function OnLoad() {
var geocoder = new google.maps.Geocoder();
if (navigator.geolocation) {
document.body.innerHTML += 'Calling <code>navigator.geolocation.getCurrentPosition</code><br />';
navigator.geolocation.getCurrentPosition(function(position) {
document.body.innerHTML += 'position.coords.latitude ' + position.coords.latitude + '<br />';
document.body.innerHTML += 'position.coords.longitude ' + position.coords.longitude + '<br />';
document.body.innerHTML += 'position.coords.accuracy ' + position.coords.accuracy + '<br />';
document.body.innerHTML += 'position.coords.altitude ' + position.coords.altitud + '<br />';
document.body.innerHTML += 'position.coords.altitudeAccuracy ' + position.coords.altitudeAccuracy + '<br />';
document.body.innerHTML += 'position.coords.heading ' + position.coords.heading + '<br />';
document.body.innerHTML += 'position.coords.speed ' + position.coords.speed + '<br />';
document.body.innerHTML += '<a href="http://maps.google.com/maps?q=' + position.coords.latitude + ',+' + position.coords.longitude + '>Open Location in Google Maps</a><br />';
geocoder.geocode({'latLng': new google.maps.LatLng(position.coords.latitude, position.coords.longitude)}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
document.body.innerHTML += 'google.maps.Geocoder ' + results[0].formatted_address;
}
} else {
document.body.innerHTML += 'Geocoder failed due to: ' + status;
}
});
}, function() {
document.body.innerHTML += 'Cannot find location' + '<br />';
});
} else {
document.body.innerHTML += 'Cannot find location' + '<br />';
}
}
Similar to the Geolocation with Google AJAX APIs we have to test whether the navigator.geolocation variable exists, this is done on Line 4.
If it exists. We proceed to call the navigator.geolocation.getCurrentPosition method. getCurrentPosition takes 3 arguments, with the last 2 being optional. For more details about the navigator.geolocation.getCurrentPosition visit the W3C Geolocation API Specification. The first two arguments accept a function to callback.
The first argument specifies whether the it was successful in finding the location.
The second argument handles any errors that may occur. For example, time outs.
Reverse Geocoding
geocoder.geocode({'latLng': new google.maps.LatLng(position.coords.latitude, position.coords.longitude)}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
document.body.innerHTML += 'google.maps.Geocoder ' + results[0].formatted_address;
}
} else {
document.body.innerHTML += 'Geocoder failed due to: ' + status;
}
});
Once again on lines 15 to 23, I use the Google Maps API to reverse geocode the address based on the supposed latitude and longitude of the user.
I changed line 15 to use variables used for latitude and longitdue. Otherwise, its exactly the same as the Geolocation with Google AJAX API.
Conclusion
That wasn't so hard now was it? Why don't you give your location aware browser a try?
