Geolocation getCurrentPosition() 方法
定义和用法
getCurrentPosition() 方法返回设备的当前位置。
实例
获取用户所在位置的经纬度:
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;
}
语法
navigator.geolocation.getCurrentPosition(success, error, options)
参数
| 参数 | 描述 |
|---|---|
| success | 必需。将 Position 对象作为其唯一输入参数的回调函数。 |
| error | 可选。将 PositionError 对象作为其唯一输入参数的回调函数。 |
| options | 可选。PositionOptions 对象。 |
返回值
设备的当前位置。
浏览器支持
| Chrome | Edge | Firefox | Safari | Opera |
|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Opera |
| 5.0 | 9.0 | 3.5 | 5.0 | 16.0 |