Done in 60 seconds: where am I (lat/long) ?

×

Error message

  • Deprecated function: The each() function is deprecated. This message will be suppressed on further calls in _menu_load_objects() (line 569 of /home/flinkco/public_html/includes/menu.inc).
  • Deprecated function: implode(): Passing glue string after array is deprecated. Swap the parameters in drupal_get_feeds() (line 394 of /home/flinkco/public_html/includes/common.inc).

The W3C Geolocation API is rapidly becoming the standard as a single common front for a web page to talk to the browsing device's position locator hardware and software. Whether the page is displayed on a desktop, laptop, smartphone or other fixed or mobile device, the Geolocation API will retrieve latitude and longitude coordinates using whatever means the device deems fit to use. The means include your IP address, your wireless network connection, nearby cell towers your phone is talking to, or dedicated GPS hardware that listens to satellites in the sky.
Below is the result for the browser and device you are currently using to view this page.

Your are here: ........

[Note: reasons why the dots above may not get replaced by your lat/long coordinates after a few seconds are when you declined to share your location (error code 1) or when you use a pre-paid internet key. Another reason is when you're reading this as part of a feed. In this case, please continue reading the article here.]

While the W3C Geolocation API is certainly the future, legacy equipment and software will linger for a while. Internet Explorer for instance only supports the W3C API from version 9.0, which for many a corporation is not yet a realistic option for their Standard Operating Environment.

A lot of older devices and software may be handled by Google Gears, which in fact was a model for the W3C.

Now there is a dead-easy way to seemlessly handle both the old and the new with the same code snippet. By importing the geo.js script into your page a couple of lines of code will give you latitude and longitude coordinates of the browsing device no matter how old or modern it is, and regardless of whether it implements the W3C Geolocation API, Google Gears or some native interface (e.g. Palm and Blackberry).

The source code behind this page is replicated below. See how simpe it is? Why not take it a notch further and also show a map of the user's location.

<script src="http://geo-location-javascript.googlecode.com/svn/trunk/js/geo.js"></script>
<script>
  if (geo_position_js.init()) {
    geo_position_js.getCurrentPosition(displayPosition, displayError, {enableHighAccuracy: true});
  }
  else {
    alert("Error: geo_position_js is not available.");
  }

  function displayPosition(position) {
    coords = position.coords;
    div = document.getElementById("position-wrapper");
    div.innerHTML = "latitude " + coords.latitude + ", longitude " + coords.longitude 
      + " (accuracy: " + coords.accuracy + " meters)";
  }
 
  function displayError(error) {
    alert("Error code: " + error.code);
  }
</script>

View more tips and tricks...