Tag Archives: HTML5

Locating current position on map

HTML5 introduced new set of specifications which include geolocation APIs. Using them one can find the location of a device. While exploring it further, I found a simple Javascript program that neatly demonstrates the overall idea (references (i)). The following code has been modified a bit from the original one to make it more verbose.

  if(navigator.geolocation) {


      // success callback, where 'position' holds the found coordinates.
      function(position) {
      // Instantiate a Google Map's LatLng object using the position coordinates.
      initialLocation = new google.maps.LatLng(position.coords.latitude,
        Now 'initialLocation' can be used to place a marker on the map or
        other coordinates related stuff.

      // error callback, where 'positionError' holds various error related
      // attributes (see reference (ii) for details).
      function(positionError) {
      // log the error message
      console.log("Geolocation service failed. MSG: " + positionError.message);
      {maximumAge:Infinity, timeout:10000});

  // Browser doesn't support Geolocation
  else {
    console.log("Your browser doesn't support geolocation.");

i) https://developers.google.com/maps/articles/geolocation
ii) http://dev.w3.org/geo/api/spec-source.html