How to access Geolocation Using the Browser

Go here to see a demo, or first read through the annotated code:
Almost all modern browsers support a geolocation query via a JavaScript call to the navigator.geolocation.getCurrentPosition function. Each browser enforces a degree of security when the call is executed. The user must give the browser permission to perform the query. In some cases, for example with some versions of Chrome, an additional security restriction may be applied that disallows the execution of this call if being run from a local file. Example code follows.
<!DOCTYPE html>
<html>
<head>
<title>Your Geolocation</title>
<meta charset="utf-8">
<script type="text/javascript">

  function getGeolocation() {
    if (window.navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
    } else {
      alert("Geolocation query not supported in this browser.");
    }
  }

  function successCallback (loc) {
    var spnLatitude = document.getElementById("latitude");
    spnLatitude.innerHTML = loc.coords.latitude;
    var spnLongitude = document.getElementById("longitude");
    spnLongitude.innerHTML = loc.coords.longitude;
  }

  function errorCallback(error) {
    alert("An error was encountered while attempting to query your geolocation through the browser: \n\n" + error.message);
  }
</script>
</head>
<body>
  <h3>Get Your Current Position via Browser Geolocation Query</h3>
  <br />
  <table>
    <tr>
      <td>Latitude:</td>
      <td><span id="latitude"></span></td>
    </tr>
    <tr>
      <td>Longitude:</td>
      <td><span id="longitude"></span></td>
    </tr>
  </table>
  <br />
  <input type="button" value="Get Your Geolocation" onclick="getGeolocation()"/>
</body>
</html>
      
The example code above is fairly straightforward and represents a simple useage of the getCurrentPosition API. The call to getCurrentPosition takes two callbacks, one for a successful execution of the function and one for an error scenario. The returned error object for the error callback also has a code property that is not used here.
A literal object may be passed as an additional third argument may also be passed to getCurrentPosition. This object allows you to specify the following three properties as paramters: enableHighAccuracy (boolean), timeout (milliseconds) and maximumAge (milliseconds). Usage of this third argument may run afoul of additional security restrictions and can cause the call to fail. It is also worth mentioning that the return object of the success callback argument, "loc", has additional properties not used in this example, though some are not relevent to a stationary browswer and only one, "accuracy" is guaranteed to exist in all implementations. The full list of return properties is given below:

You can see this code in action here.