要获取当前用户位置信息,可以使用Geolocation API中的getCurrentPosition方法,将其作为回调函数的参数,同时在回调函数中获取位置信息,如下所示:
navigator.geolocation.getCurrentPosition(function(position) {
var lat = position.coords.latitude; //纬度
var lng = position.coords.longitude; //经度
var accuracy = position.coords.accuracy; //精确度
// ...
});
在以上代码中,getCurrentPosition方法用于获取当前设备的位置信息,position参数包含了位置信息,可以通过position.coords获取纬度、经度和精确度等相关信息。将获取到的经纬度信息保存到变量中即可实现对位置信息的处理和运用。