Thursday 28 November 2013

How to use Geolocation API in HTML5

If you want to use Geolocation API in HTML5 , used to get geographical position by the user. 
It can be support by many browser now ,

Internet Explorer Firefox Opera Google Chrome Safari

Internet Explorer 9+, Firefox, Opera, Chrome and Safari are support Geolocation

The Example is : 



<!DOCTYPE html>
<html>
<body>
<p> your coordinates is :</p>
<script>
 if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(showPosition);
    }
  else{x.innerHTML="Geolocation is not supported by this browser.";}
function showPosition(position)
  {
  x.innerHTML="Latitude is : " + position.coords.latitude + 
  "<br>Longitude is: " + position.coords.longitude;
  }
</script>
</body>
</html>
You Will Get Your Latitude and Longitude .

If You want to show your latitude and longitude in Google Map
Then you want to use Google Map API.

You Will Get Compelete Source Code Here .


<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0; padding: 0 }
      #map-canvas { height: 100% }
    </style>
    <script type="text/javascript"
      src="https://maps.googleapis.com/maps/api/js?key=API_KEY&sensor=SET_TO_TRUE_OR_FALSE">
    </script>
    <script type="text/javascript">
      function initialize() {                                                                    navigator.geolocation.getCurrentPosition(showPosition);                                                                   var  Latitude =  position.coords.latitude;                                                                                       var Longitude = position.coords.longitude;                                         
 var mapOptions = {
          center: new google.maps.LatLng(Latitude, Longitude),
          zoom: 8
        };
        var map = new google.maps.Map(document.getElementById("map-canvas"),
            mapOptions);
      }
      google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>
    <div id="map-canvas"/>
  </body>
</html>