目录

Geolocation getCurrentPosition() 方法

示例

获取用户位置的纬度和经度:

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;
}
亲自试一试 »

描述

getCurrentPosition() 方法返回设备的当前位置。


浏览器支持

Method
getCurrentPosition() 5.0 9.0 3.5 5.0 16.0

语法

navigator.geolocation.getCurrentPosition( success, error, options)

参数值

Parameter Type Description
success   Required. A callback function that takes a Position object as its sole input parameter
error   Optional. A callback function that takes a PositionError object as its sole input parameter
options   Optional. A PositionOptions object

返回值

设备的当前位置