Home  >  Article  >  Web Front-end  >  HTML DOM Geolocation coordinates attribute

HTML DOM Geolocation coordinates attribute

WBOY
WBOYforward
2023-09-06 20:05:07670browse

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.

Properties

The following are the coordinate properties -

Note - All these properties are read-only and have a return type of double.

##567
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.

Syntax

The following is the syntax for the GeoLocation coordinate attribute -

coordinates.property

The "attribute" can be one of the above mentioned attributes in the table one.

Example

Let 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&#39;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>

Output

This will produce the following output-

HTML DOM Geolocation coordinates属性

When you click the "Coordinates" button and click "Allow" in the "Know your location" popup -

HTML DOM Geolocation coordinates attribute

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&#39;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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete