How to Find Geographical Location Using HTML 5

Geo locating is one of the coolest feature offered by HTML5. Its very easy to implement and helps the website owners to find the location of their visitor and server relative content. This feature doesn’t aim at endangering user’s privacy by asking to confirm their tracking. This feature’s accuracy is good when it comes to locating city but exact location within the city can’t be guaranteed.

We will be using Google Maps’ API for mapping the location. So lets get started.

The following javascript function is used for getting latitude and longitude of the user’s location.

function getLocation()

  {

  if (navigator.geolocation)

    {

    navigator.geolocation.getCurrentPosition(showPosition,showError);

    }

  else{x.innerHTML="Geolocation is not supported by this browser.";}

  }



Then the below function will display the location using Google Maps.

function showPosition(position)

  {

  var latlon=position.coords.latitude+","+position.coords.longitude;



  var img_url="http://maps.googleapis.com/maps/api/staticmap?center="

  +latlon+"&zoom=14&size=400x300&sensor=false";

  document.getElementById("mapholder").innerHTML="<img src='"+img_url+"' />";

  }



While performing this, there is also a necessity to handle errors. So we use the following function.

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;

    }

  }



Entire code with HTML

<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to find your location:</p>
<button onclick="getLocation()">Find Location</button>
<div id="mapholder"></div>
<script>
var x=document.getElementById("demo");
function getLocation()
  {
  if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(showPosition,showError);
    }
  else{x.innerHTML="Geolocation is not supported by this browser.";}
  }

function showPosition(position)
  {
  var latlon=position.coords.latitude+","+position.coords.longitude;

  var img_url="http://maps.googleapis.com/maps/api/staticmap?center="
  +latlon+"&zoom=14&size=400x300&sensor=false";
  document.getElementById("mapholder").innerHTML="<img src='"+img_url+"' />";
  }

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>
</body>
</html>

Live Demo

Recommended Posts

No comment yet, add your voice below!


Add a Comment

Your email address will not be published. Required fields are marked *