Thuộc tính tọa độ vị trí địa lý
Thí dụ
Nhận vĩ độ và kinh độ của vị trí của người dùng:
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
Định nghĩa và Cách sử dụng
Thuộc tính tọa độ trả về vị trí và độ cao của thiết bị trên Trái đất.
Thuộc tính tọa độ
Property | Description |
---|---|
coordinates.latitude | Returns the position's latitude in decimal degrees |
coordinates.longitude | Returns the position's longitude in decimal degrees |
coordinates.altitude | Returns the position's altitude in meters, relative to sea level |
coordinates.accuracy | Returns the accuracy of the latitude and longitude properties in meters |
coordinates.altitudeAccuracy | Returns the accuracy of the altitude property in meters |
coordinates.heading | Returns the direction in which the device is traveling. This value, specified in degrees, indicates how far off from heading true north the device is. 0 degrees represents true north, and the direction is determined clockwise (east is 90 degrees and west is 270 degrees). If speed is 0, heading is NaN. If the device is unable to provide heading information, this value is null |
coordinates.speed | Returns the velocity of the device in meters per second. This value can be null |
Hỗ trợ trình duyệt
Property | |||||
---|---|---|---|---|---|
coordinates | 5.0 | 9.0 | 3.5 | 5.0 | 10.6 |
❮ Đối tượng định vị