Skip to content
Related Articles

Related Articles

HTML | Navigator geolocation Property
  • Last Updated : 26 Jul, 2019

The Navigator geolocation property is used to return a geolocation object by the browser which can be used to locate a user’s position.
It is a read-only property and is only available on the approval of the user since it can compromise the user’s privacy.

Syntax:

navigator.geolocation

Below program illustrates the Navigator geolocation Property :

Checking the user’s geolocation.




<!DOCTYPE html>
<html>
  
<head>
    <title>Navigator geolocation Property in HTML</title>
    <style>
        h1 {
            color: green;
        }
          
        h2 {
            font-family: Impact;
        }
          
        body {
            text-align: center;
        }
    </style>
</head>
  
<body>
  
    <h1>GeeksforGeeks</h1>
    <h2>Navigator geolocation Property</h2>
  
    <p>For checking your coordiates, 
      double click the "Get Coordinates" button : </p>
  
    <button ondblclick="getCoordinates()">Get Coordinates</button>
  
    <p id="loc"></p>
  
    <script>
        var x = document.getElementById("loc");
  
        function getCoordinates() {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(showcoordinates);
            } else {
                x.innerHTML = "The browser doesn't support Geolocation.";
            }
        }
  
        function showcoordinates(myposition) {
            x.innerHTML = "Your Latitude is: " + myposition.coords.latitude +
                "<br>Your Longitude is: " + myposition.coords.longitude;
        }
    </script>
  
</body>
  
</html>

Output:

After clicking the button

Supported Browsers: The browser supported by Navigator geolocation are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari



My Personal Notes arrow_drop_up
Recommended Articles
Page :