【问题标题】:Why callbacks function are not fired?为什么不触发回调函数?
【发布时间】:2020-06-02 02:10:49
【问题描述】:

我有这个功能来指示是否在浏览器上启用了地理定位:

function geolocationInfo() {

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(success, error);
    }
    else {
        return "Geolocation not works in this browser";
    }
}

  function success(position) {
        var crd = position.coords;

        console.log('Ваше текущее метоположение:');
        console.log(`Широта: ${crd.latitude}`);
        console.log(`Долгота: ${crd.longitude}`);
        console.log(`Плюс-минус ${crd.accuracy} метров.`);
    }

    function error(error) {
        switch (error.code) {
            case error.PERMISSION_DENIED:
                return "Geolocation disabled";
            case error.POSITION_UNAVAILABLE:
                return "GeoLocation is not available";
            case error.TIMEOUT:
                return "Time is ends";
            case error.UNKNOWN_ERROR:
                return "error.";               
        }
    }

问题是回调函数没有被触发。

知道为什么不触发回调(成功和错误)吗?

【问题讨论】:

  • 通常浏览器会要求用户允许/拒绝地理定位 - 在为该站点做出或记住其中一个选择之前不会调用回调 - 那么,控制台中根本没有其他错误吗?顺便问一下,哪个浏览器?页面是 https 还是 http?

标签: javascript geolocation gps location gis


【解决方案1】:

如果您尝试在 chrome 的控制台中使用您的代码,并运行您的函数

geolocationInfo()

然后在浏览器中检查具有权限的弹出窗口,因为用户必须允许网页查看位置

【讨论】:

  • 非常感谢,我只是尝试配置为什么我的代码不起作用
  • Michael,您的代码对我来说可以正常工作(Chrome 83.0.4103.61),只要您点击“允许”或“阻止”,您就会看到回调结果。
猜你喜欢
  • 2014-11-05
  • 1970-01-01
  • 1970-01-01
  • 2017-03-15
  • 2011-11-23
  • 2015-03-29
  • 2012-06-20
  • 1970-01-01
  • 2014-11-29
相关资源
最近更新 更多