Starting with geolocation api in HTML 5

Geo location in HTML5

Geolocation is the HTML 5 API that is used to get the location of the user. You can get the latitude and longitude of the user’s location.

But the question is why this service is so useful? I will try to explain let’s suppose you are travelling a city and you have not adequate information about the routes, public transport system and other places. In this situation many web applications or your phone can help you to get the routes, nearest bus or train station and the information of the place where you have to visit by using the geolocation services. They get your location and provide you all possible information.

Twitter also allows users to geotag their tweets so that you can read their tweet and get about their location at that time. I also use the Here Maps application in my Nokia smart phone.

Today’s smartphones, Android devices, services such as Google Maps for mobile, Urban spoon and Yelp (Yahoo partnering with Yelp to improve local search results) use location of the user to provide relevant local search results.
Many companies and service providers also use the geolocation to maintain their supply demand, customer experience by using improve the service on their location.

The geolocation API provides a property geolocation in the global object navigator( that also contains the information about the browser).

Let’s see the following example to get the user current location:

<!DOCTYPE html>
function getLocation(){
function printLocation(position)
  var _coords = position.coords;
  document.write('Your latitude: ' + _coords.latitude );
  document.write('longitude:' + _coords.longitude );
  <button onclick="getLocation();">Try It</button>

Browser Support

The current versions of Internet Explorer, Chrome, Firefox, Safari and Opera support geolocation. Check the version of various browser implementing geolocation api:

Internet Explorer 

Workaround to detect the Browser support

Generally we can write the code to check whether browser supports HTML5 GeoLocation API or not like as:

if (navigator.geolocation) {
  // Write your code
} else {
  alert('Geo Location is not supported');

You can also use the Modernizr a open source library in your application. You need just add the reference in your page.
Modernizr creates a JavaScript object called called Modernizr, which has a list of properties that contain boolean values for each feature. foe example Modernizr.geolocation property will be true if the user browser supports the geolocation api, and false if the browser does not support it.

You can use like this:

if (Modernizr.geolocation) {
  // Write your code
} else {
  alert('Geo Location is not supported');

Properties of the getCurrentPosition() method

getCurrentPosition() methods returns an position object that contain two properties: coords and timestamp. The coords object has the following properties:

latitude : get the latitude in decimal number
longitude : get the longitude in decimal number
accuracy : get the accuracy of position
altitude : get altitude in meters
altitudeAccuracy : get the altitude accuracy of position
heading : get the degrees clockwise from North
speed : get the in meters per second

And timestamp refers the date and time when the location was calculated.


Browser does not give your location to websites without user permission. This is the security concern. Generally when the web application or pages request to share you location, the browser notify to the user for the permission. It depends on the browser to browser that how they notify you. You can get an idea from the below picture.

Requesting location permission by browsers

For More information on geolocation security: see Security and privacy considerations.


Use with Google Map

Click here to show your location in Google Map:


HTML Code:

The following example get the current position of the user and use the Google Map to show you.

<!DOCTYPE html>
    <title>Current location in Google Map</title>
   <script src=""></script>
function showMyLocation() {
  if (navigator.geolocation) {
    else { alert("no supported"); }
  function showMap(position)
     var lati = position.coords.latitude;
     var longi = position.coords.longitude;
     var pos = new google.maps.LatLng(lati,longi);
     var options = {
       zoom: 8,
       center: pos,
       mapTypeControl: false,
       navigationControlOptions: {
    	  style: google.maps.NavigationControlStyle.SMALL
       mapTypeId: google.maps.MapTypeId.ROADMAP
     var myDiv = document.getElementById("mapDiv");
     var myMap = new google.maps.Map(myDiv, options);
     var infowindow = new google.maps.InfoWindow({
        map: myMap,
        position: pos,
        content: 'It seems you are here.'
   <button onclick="showMyLocation();"> Show My Location </button>
    <div id="mapDiv"></div>

Use with Bing Map