获取位置以在控制台中显示

时间:2021-04-24 20:24:44

I am trying to get the latitued and longitude to print to the console, but nothing is showing with the code I have. It seems to work in JSfiddle, but not on my desktop.

我试图将latitued和经度打印到控制台,但没有显示我的代码。它似乎适用于JSfiddle,但不适用于我的桌面。

function success(position) {
    var latitude  = position.coords.latitude;
    var longitude = position.coords.longitude;
    console.log(latitude, longitude);
    }


  navigator.geolocation.getCurrentPosition(success);

Edit: https://jsfiddle.net/gf0gmhqv/

1 个解决方案

#1


0  

Like @M4tini said, works fine. Problem is, why doesn't it do that for you?

喜欢@ M4tini说,工作正常。问题是,为什么不为你这样做?

You can find out with the new permission api (for firefox and blink only) if it is granted, denied or prompt

如果授予,拒绝或提示,您可以找到新的权限api(仅适用于firefox和blink)

You can also use the error callback to find out if there where any problem getting the position

您还可以使用错误回调来确定是否有任何问题获得该位置

navigator.permission.query({name: 'geolocation'}).then(permission => {
    alert(permission.state)
})

navigator.geolocation.getCurrentPosition(success, error);

function error(err) {
    console.warn('ERROR(' + err.code + '): ' + err.message)
}

#1


0  

Like @M4tini said, works fine. Problem is, why doesn't it do that for you?

喜欢@ M4tini说,工作正常。问题是,为什么不为你这样做?

You can find out with the new permission api (for firefox and blink only) if it is granted, denied or prompt

如果授予,拒绝或提示,您可以找到新的权限api(仅适用于firefox和blink)

You can also use the error callback to find out if there where any problem getting the position

您还可以使用错误回调来确定是否有任何问题获得该位置

navigator.permission.query({name: 'geolocation'}).then(permission => {
    alert(permission.state)
})

navigator.geolocation.getCurrentPosition(success, error);

function error(err) {
    console.warn('ERROR(' + err.code + '): ' + err.message)
}

相关文章