Strange Symphonies The best way to predict the future is to invent it

13Nov/090

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?

1Sep/068

Protecting your Privacy: Setting up Tor in Ubuntu Dapper Drake

After Merdeka, I've decided to set up Tor. There is alot of talk about freedom, but the truth is freedom is more likely to be taken away, than given. Simliarly there are certain topics in Malaysia which are best avoided, and considered taboo to talk about.

For example: China has its infamous great firewall. The Chinese government does not permit the population to view certain sites such as Wikipedia. Though the Chinese government may have their reasons, I believe this act of censorship is immoral.

So you've decided to surf the web anonymously? Luckily Tor can do the job for you! The truth is when your on the web, you are being tracked, every single page you load is being cached or logged somewhere. Tor is an application engineered to protect your privacy, through the use of its own network of nodes designed to increase anonymity.

For example, since I am a citizen of Malaysia, by going directly to http://www.google.com I get rerouted to http://www.google.com.my, the Malaysian version for Google. Why is this? How do they do I originate from Malaysia? Well I won't answer that question here, but as you can see they know where you live. Bet you can't sleep with your eyes open now can you?

Well with Tor installed, and when accessing google.com, I get the UK version! My connection is being routed from the UK to my place. This changes everytime you go to a new website so the point of exit (in this case UK) is always different.

Here are the basic instructions to set up Tor, and you can learn more about Tor from their overview, or our good friend Wikipedia.

I will not be held responsible for the actions conducted by any person using these instructions. Do this at your own risk. Similarly when Tor starts it warns the user: This is experimental software. Do not rely on it for strong anonymity.

How to set up Tor in Ubuntu Dapper Drake:

These instructions only apply if you have the universe repo installed. Not set up? Find out how to set up the extra repositories in Ubuntu

  1. Run the command: apt-get install tor privoxy
  2. Run the command: sudo gedit /etc/privoxy/config
  3. Add the line (including the period at the end): forward-socks4a / localhost:9050 .
  4. Comment out the line: logfile logfile
  5. Comment out the line: jarfile jarfile
  6. Restart the Privoxy service: sudo /etc/init.d/privoxy restart

How to set up Firefox to work with Tor:

  1. Install the torbutton extension.
  2. Restart Firefox
  3. In the bottom right corner you'll have a text blurb saying: Tor Disabled
  4. Enable Tor by clicking the blurb; it should say: Tor Enabled
  5. Surf the web

You can customize the blurb so that its a nicer looking icon. Right click the blurb, go to Preferences, and set Status Bar Display Format to Icon.

You will also notice that your connection is slower; taking longer to load webpages. This is one of the disadvantages of Tor as it has to route your connections to different places.

Of course if your accessing services across the net using your account, perhaps Gmail. They do know who you are, but they won't be able to judge where you are accessing from. So if you want to really be anonymous, becareful of your activity.

So go ahead, use Tor when you are in an unsafe situation. When you don't trust the network or geographical location you are in. Perhaps you want to access a site that has blocked off all computers in a given area, and your included. Maybe you just want to surf a little safer while drinking a cup of coffee at Starbucks. Or just maybe you don't want to be tracked. At least you know Tor is a click away.

I'll put up instructions on how to set up more services later.

In the words of Victor Hugo:

"There is one thing stronger than all the armies in the world, and that is an idea whose time has come."