navigator.geolocation.watchPosition成功回调总是使用空位置对象调用。我错过了什么?尝试了以下设置(已授予位置,https):
- Chrome桌面95.0.4638.54
- Chrome安卓94.0.4606.85
function errorCb(error) {
console.log('errorCb() ' + error.code + ", " + error.message);
}
if (navigator.geolocation) {
const options = {
enableHighAccuracy: false,
timeout: 5000,
maximumAge: 0
};
navigator.geolocation.watchPosition(position => {
console.log("pos: " + JSON.stringify(position)); // always displays "pos: {}"
}, errorCb, options);
}
参考Andys注释,所提供的位置对象无法字符串化。直接访问对象:
var coords = position.coords;
var latitude = coords.latitude;
var longitude = coords.longitude;