地理定位 coordinates 属性
示例
获取用户位置的纬度和经度
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "此浏览器不支持地理定位。";
}
}
function showPosition(position) {
x.innerHTML = "纬度: " + position.coords.latitude +
"<br>经度: " + position.coords.longitude;
}
自己动手试一试 »
描述
coordinates 属性返回设备在地球上的位置和海拔高度。
Coordinates 属性
| 属性 | 描述 |
|---|---|
| coordinates.latitude | 返回位置的纬度(十进制度数) |
| coordinates.longitude | 返回位置的经度(十进制度数) |
| coordinates.altitude | 返回位置的海拔高度(单位:米),相对于海平面 |
| coordinates.accuracy | 返回纬度和经度属性的精度(单位:米) |
| coordinates.altitudeAccuracy | 返回海拔高度属性的精度(单位:米) |
| coordinates.heading | 返回设备行驶的方向。此值以度为单位,表示设备偏离正北的方向。0 度表示正北,方向按顺时针确定(东为 90 度,西为 270 度)。如果速度为 0,则 heading 为 NaN。如果设备无法提供方向信息,则此值为 null。 |
| coordinates.speed | 返回设备的速度(单位:米/秒)。此值可以为 null。 |
浏览器支持
| 属性 | |||||
|---|---|---|---|---|---|
| coordinates | 5.0 | 9.0 | 3.5 | 5.0 | 10.6 |