Home >Web Front-end >HTML Tutorial >HTML DOM Geolocation coordinates attribute
HTML DOM geolocation coordinates attribute is used to obtain the location and altitude of the user device on the earth. The user must approve the coordinates he wants to provide for this property to work. This is done so as not to compromise user privacy. This can be used to track the location of various devices.
The following are the coordinate properties -
Note - All these properties are read-only and have a return type of double.
Sr.No th> | Properties and Description |
---|---|
1 |
coordinates.latitude Returns the latitude of the device location in decimal degrees. |
2 |
Coordinates.Longitude Returns the longitude of the device location in decimal degrees |
3 |
coefficients.altitude Returns the location's altitude in meters, relative to sea level. Can return null if there is no GPS in the device. |
4 |
Coordinates. Accuracy Returns the accuracy of the latitude and longitude attributes in meters |
coordinates.altitudeAccuracy Returns the accuracy of the altitude attribute in meters |
|
cocos.headingReturn the direction of travel of the device. This value, in degrees, represents the device's distance from a heading due north. 0 degrees represents true north, and the direction is determined clockwise (east is 90 degrees, west is 270 degrees). If the speed is 0, the heading is NaN. If the device cannot provide heading information, the value is null | |
Coordinates.speed Returns the speed of the device (in meters per second). The value can be null. |
coordinates.propertyThe "attribute" can be one of the above mentioned attributes in the table one. ExampleLet us see an example of the GeoLocation coordinate attribute-
<!DOCTYPE html> <html> <body> <h1>Geolocation coordinates property</h1> <p>Get you coordinates by clicking the below button</p> <button onclick="getCoords()">COORDINATES</button> <p id="Sample">Your coordinates are:</p> <script> var p = document.getElementById("Sample"); function getCoords() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showCoords); } else { p.innerHTML ="This browser doesn't support geolocation."; } } function showCoords(position) { p.innerHTML = "Longitude:" + position.coords.longitude + "<br>Latitude: " + position.coords.latitude+"<br>Accuracy: "+ position.coords.accuracy; } </script> </body> </html>OutputThis will produce the following output- When you click the "Coordinates" button and click "Allow" in the "Know your location" popup - above In the example - We first created a button COORDINATES which will execute the getCoords() method when the user clicks it -
<button onclick="getCoords()">COORDINATES</button>getCoords() function gets the geolocation property of the navigator object to check Whether the browser supports geolocation. If the browser supports geolocation, it will return a Geolocation object. Using the getCurrentPosition() method of the navigator's geolocation property, we can get the current location of the device. The getCurrentPosition() method is a callback function that accepts a function object as its argument, since every function is an object in JavaScript. Here, we pass the showCoords() method to it. The showCoords() method takes a location interface as a parameter and uses it to display the longitude, latitude, and precision within the paragraph with id "Sample". It uses the paragraph innerHTML attribute to append text to it -
function getCoords() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showCoords); } else { p.innerHTML ="This browser doesn't support geolocation."; } } function showCoords(position) { p.innerHTML = "Longitude:" + position.coords.longitude + "<br>Latitude: " + position.coords.latitude+"<br>Accuracy: "+ position.coords.accuracy; }
The above is the detailed content of HTML DOM Geolocation coordinates attribute. For more information, please follow other related articles on the PHP Chinese website!