Skip to content
Related Articles

Related Articles

HTML | DOM Geolocation coordinates Property
  • Last Updated : 23 Jul, 2019

The DOM Geolocation coordinates Property in HTML is used to return the position and altitude of the device on Earth. The returned Coordinates object could be used for various purposes including navigation and tracking the position of the device.

Property Values:

ValuesDescription
coordinates.latitudeThis is the device latitude in decimal degrees.
coordinates.longitudeThis is the device longitude in decimal degrees.
coordinates.accuracyThis is the accuracy of the latitude and longitude returned in meters.
coordinates.altitudeThis is the device altitude relative to the sea level in meters.
coordinates.altitudeAccuracyThis is the accuracy of the altitude returned in meters.
coordinates.headingThis is the direction in which the device is traveling. The value is in degrees and indicates the current direction with respect to true north. This value may be null.
coordinates.speedThis is the velocity of the device in meters per second. The value may be null if the device is not traveling.

Usage: Methods like getCurrentPosition() or watchPosition() are used to pass a callback to a function and then access the coordinates property.

Example:




<!DOCTYPE html>
<html>
<title>DOM Geolocation coordinates Property</title>
  
<body>  
  <h1 style="color: green">GeeksforGeeks</h1>
    
  <b>DOM Geolocation coordinates Property</b>
    
  <p>Click the button to get your coordinates.</p>
      
  <button onclick="getLocation()">Get Location</button>
    
  <p class="location"></p>
  
    <script>
        let x = document.querySelector('.location');
  
        function getLocation() {
  
            /* Check if location support is available */
            if (navigator.geolocation) {
  
                /* Callback to the showPosition function */
                navigator.geolocation.getCurrentPosition(
                  showPosition);
            } else {
                x.innerHTML = "Geolocation is not supported.";
            }
        }
  
        function showPosition(position) {
  
            /* Assign the Coordinates object to a variable */
            let coordinatesObject = position.coords;
  
            x.innerHTML =
                "Accuracy: " +
  
                /* Get the accuracy from the 
                Coordinates object */
                coordinatesObject.accuracy +
  
                "<br>Latitude: " +
  
                /* Get the latitude from the 
                Coordinates object */
                coordinatesObject.latitude +
  
                "<br>Longitude: " +
  
                /* Get the longitude from the 
                Coordinates object */
                coordinatesObject.longitude +
  
                "<br>Altitude: " +
  
                /* Get the altitude from the 
                Coordinates object */
                coordinatesObject.altitude +
  
                "<br>Altitude Accuracy: " +
  
                /* Get the altitude accuracy 
                from the Coordinates object */
                coordinatesObject.altitudeAccuracy +
  
                "<br>Speed: " +
  
                /* Get the speed from 
                the Coordinates object */
                coordinatesObject.speed +
  
                "<br>Heading: " +
  
                /* Get the heading from 
                the Coordinates object */
                coordinatesObject.heading;
  
        }
    </script>
</body>
  
</html>

Output:



Before clicking the button:
coords-before

After clicking the button:
coords-after

Supported Browsers: The browser supported by DOM Geolocation coordinates property are listed below:

  • Google Chrome 5.0
  • Firefox 3.5
  • Internet Explorer 9.0
  • Opera 10.6
  • Safari 5.0
My Personal Notes arrow_drop_up
Recommended Articles
Page :