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

Best Places to Start Learning HTML 5, CSS 3, Javascript, PHP, MySQL

If you are planning to continue with Web Application programming you need to have knowledge of
1. HTML 5
2. CSS 3
3. Javascript
4. PHP  &  MySQL (My preferred language and database engine)

I am going to continue to write my most of the articles related to web application programming on PHP – MySQL pair as it is my favorite and immensely dynamic.
Lets discuss each entity’s role and importance in web application programming.

1. HTML 5
Now many of you have heard about HTML. It is used for creating web pages etc.. and probably wondering what is HTML 5? Well HTML 5 is nothing but a new and advanced version of HTML. It has got tons of new features, tags etc. If you intend to excel in web application development you must conquer HTML 5 first.
Here are a few important websites that will get you started with HTML 5.
1. Tizag.com/htmlT/
2. W3Schools.com/html5/default.asp
3. html5demos.com (An excellent website for you to checkout new features of HTML 5)
4. W3.org/TR/html5/ (A detailed reference of HTML 5 and its features)

You might also wanna buy these books that contains some quick and awesome HTML 5 hacks.
1. Pro HTML5 Programming: Powerful APIs for Richer Internet Application Development
US/UK/CA                  India
 

2. HTML5 24-Hour Trainer (Wrox Programmer to Programmer)
US/UK/CA                  India
 

2. CSS3
Just as HTML 5 is new version of HTML, CSS3 is the most recent version of CSS (Cascading Style Sheet). CSS is generally used to enhance a website’s look. It has a lot of features like animations, text color, shadows, backgrounds, transitions, custom borders and its colors etc.
Here are a few important websites that will get you started with CSS3.
1.Tizag.com/cssT/
2. W3Schools.com/css3/default.asp
3. CSS3.com

You might also wanna buy these books to know extra stuff about styling your website.
1. Basics of Web Design: HTML5 and CSS3
US/UK/CA                  India
 

3. Javascript
Javascript is a browser sided scripting language. The script is downloaded on client side and executed as the calls are made. Javascript is used to enhance the interactivity and looks of a website. It is used to create drop down menus, perform run-time calculations, update content view dynamically via AJAX(A combination of XML and Javascript to update records/views dynamically at run-time). To reduce the coding work there are many Javascipt libraries like JQuery and MooTools which help you in adding some awesome effects to your website in very less time.
You can visit the following websites to start learning Javascript.
1. Ttizag.com/javascriptT/
2. W3schools.com/js/default.asp
3.Codeacademy.com

Books that can help you learn Javascript even better.
1. Professional JavaScript for Web Developers (Wrox Programmer to Programmer)
US/UK/CA                  India
 

2. JavaScript for Absolute Beginners
US/UK/CA                  India
 

4. PHP and MySQL
PHP-MySQL is the most powerful hence most preferred solution for web based application. PHP provides powerful programming flexibility while MySQL provides us with secure, fast and reliable database management facility. PHP is very easy to understand and one can quickly learn it if he/she has learned the basics of programming by studying C and C++. With ability of numerous inbuilt functions programming a web application becomes even faster. To add to this there are various  frameworks like Code Igniter, Zend, CakePHP which reduce your programming even further since a LOT of functions are available in them too. PHP is both procedural as well as Object Oriented language. The best part is we can use it with HTML. With help of HTML+Javascript+CSS you can create a jaw-dropping look while by using PHP+MySQL with HTML you can create mind blowing PHP application.

Books for PHP and MySQL
1. Beginning PHP and MySQL: From Novice to Professional
US/UK/CA                  India
 

2. PHP Solutions: Dynamic Web Design Made Easy
US/UK/CA                  India
 

3. PHP 6: A Beginner’s Guide
US/UK/CA                  India
 

So this was pretty much I could possibly help you with basics of web application programming. I hope you complete it fast and as you complete, don’t forget to go through intermediate-advanced topics I would be discussing in my upcoming programming posts.
I hope this article helps you. Share it if you like it.
Good Luck and Thank You!!