Child

HTML5 Geolocation Element



Approx three years ago when mobile android devices came in to boom, and today we are surrounded by the many of smartphones and also now we are addicted to use it. In the present scenario the smartphone is not only for the voice interaction purpose but more than that. Now with development of HTML5 We can use the tracking of Geolocation and not only the latitude displaying but also we can view the digital map with HTML5 Geolocation features. Though We can use it on browser also, but it best suit on android or smartphone.

Locate the User's Position

The HTML5 Geolocation API is used to get the geographical position of a user.

Since this can compromise user privacy, the position is not available unless the user approves it.

Browser Support:
Internet Explorer 9, Firefox, Opera, Chrome, and Safari support the <video> element.

Simple output for HTML5 Geolocation With Graph

HTML5 Geolocation is used to locate the position of current internet user. Click on the given demo button to watch the live example.

The code for above Example:

<p id="mapp"><strong>HTML5 Geolocation</strong> is used to locate the position of current internet user. Click on the given demo button to watch the live example.<button onclick="getLocation()">DEMO</button></p>
<div id="display"></div>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script><script type="text/javascript">// <![CDATA[
var x=document.getElementById("mapp");
function getLocation()
  {
  if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(showPosition,showError);
    }
  else{x.innerHTML="Geolocation is not supported by this browser.";}
  }
function showPosition(position)
  {
  lat=position.coords.latitude;
  lon=position.coords.longitude;
  latlon=new google.maps.LatLng(lat, lon)
  display=document.getElementById('display')
  display.style.height='250px';
  display.style.width='525px';
  var myOptions={
  center:latlon,zoom:14,
  mapTypeId:google.maps.MapTypeId.ROADMAP,
  mapTypeControl:false,
  navigationControlOptions:{style:google.maps.NavigationControlStyle.SMALL}
  };
  var map=new google.maps.Map(document.getElementById("display"),myOptions);
  var marker=new google.maps.Marker({position:latlon,map:map,title:"You are here!"});
  }
function showError(error)
  {
  switch(error.code)
    {
    case error.PERMISSION_DENIED:
      x.innerHTML="User denied the request for Geolocation."
      break;
    case error.POSITION_UNAVAILABLE:
      x.innerHTML="Location information is unavailable."
      break;
    case error.TIMEOUT:
      x.innerHTML="The request to get user location timed out."
      break;
    case error.UNKNOWN_ERROR:
      x.innerHTML="An unknown error occurred."
      break;
    }
  }
// ]]></script>

Location-specific Information

This page demonstrated how to show a user's position on a map. However, Geolocation is also very useful for location-specific information.

Examples:

  • Up-to-date local information
  • Showing Points-of-interest near the user
  • Turn-by-turn navigation (GPS)

The getCurrentPosition() Method - Return Data

The getCurrentPosition() method returns an object if it is successful. The latitude, longitude and accuracy properties are always returned. The other properties below are returned if available.

Property Description
coords.latitude The latitude as a decimal number
coords.longitude The longitude as a decimal number
coords.accuracy The accuracy of position
coords.altitude The altitude in meters above the mean sea level
coords.altitudeAccuracy The altitude accuracy of position
coords.heading The heading as degrees clockwise from North
coords.speed The speed in meters per second
timestamp The date/time of the response