Home / DeveloperSection / Articles / HTML5 Geolocation

HTML5 Geolocation

AVADHESH PATEL 6323 16-Oct-2012

HTML5 Geolocation is used to locate a user's position

Note: 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.

For getting user location, API use getCurrentPosition() method. This method gets longitude and latitude points of user. Latitude and longitude is a coordinate that enables every location on the Earth to be specified by a set of numbers and/or letters.

Here we demonstrate to pick current position (Latitude and Longitute) of user.

<!DOCTYPE html>

<title>Get latitude and longitude</title>
<p id="latLog"></p>
<script type="text/javascript">
    var x = document.getElementById("latLog");
    window.onload = function getLocation() {
        if (navigator.geolocation) {
        else { x.innerHTML = "Geolocation is not supported by this browser."; }
    function showPosition(position) {
        x.innerHTML = "Latitude: " + position.coords.latitude +
  "<br>Longitude: " + position.coords.longitude;

Latitude: 25.450001
Longitude: 81.849998

Note: Latitude and longitude values are differing for every user.

Now these values (latitude and longitude) we pass to goggle map API that display UI position of user.

HTML5 - Using Geolocation 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
    <script type="text/javascript" src=""></script>
    <script type="text/javascript">
        var x = document.getElementById("ErrorMsg");
      window.onload= 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)
            mapholder = document.getElementById('mapholder')
   = '200px';
   = '600px';
            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("mapholder"), 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."
                case error.POSITION_UNAVAILABLE:
                    x.innerHTML = "Location information is unavailable."
                case error.TIMEOUT:
                    x.innerHTML = "The request to get user location timed out."
                case error.UNKNOWN_ERROR:
                    x.innerHTML = "An unknown error occurred."
<p id="ErrorMsg"></p>
<div id="mapholder" style="border:2px solid black;"></div>

 Screen Shot

HTML5 Geolocation

Handling Errors and Rejections

In the above code we used getCurrentPosition() method is used to handle errors. It specifies a function to run if it fails to get the user's location.

Meaning of Error Codes:

 ·        Permission denied - The user did not allow Geolocation

 ·         Position unavailable - It is not possible to get the current location

 ·         Timeout - The operation timed out

The other properties below are returned if available.



The latitude as a decimal number


The longitude as a decimal number


The accuracy of position


The altitude in meters above the mean sea level


The altitude accuracy of position


The heading as degrees clockwise from North


The speed in meters per second


The date/time of the response

Geolocation object - Other interesting Methods

watchPosition() - Returns the current position of the user and continues to return updated position as the user moves (like the GPS in a car).

clearWatch() - Stops the watchPosition() method.

Updated 07-Sep-2019
Avadhesh Kumar Patel District Project Manager - Aligarh 14 months work experience in Panchayati Raj Department Sector as District Project Manager & 12 months work experience in IT Sector as Software Engineer. :-)

Leave Comment

Please enter subject.
Please enter comments message.


Liked By