【问题标题】:getCurrentPosition() not working in firefoxgetCurrentPosition() 在 Firefox 中不起作用
【发布时间】:2014-02-09 16:26:19
【问题描述】:

我正在尝试使用 navigator.geolocation.getCurrentPosition() 函数进行地理定位,但我似乎无法让它在 Firefox 上运行。 更具体地说,我制作了一个非常简单的 HTML 文件:

<!DOCTYPE HTML>
<html>
<head>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 
<script type="text/javascript" src="js/mapHandler.js"></script>
</head>
<body>
</body>
</html>

如您所见,它是 HTML 文件的最低限度。 javascript 只是执行以下操作:

function displayLocation (position)
{
alert("displayLocation");
}
function displayError(positionErr)
{
alert("error");
}
function initialize()
{
 if (!(navigator.geolocation == 'undefined'))
  {
   navigator.geolocation.getCurrentPosition(displayLocation, displayError,{timeout:10000});
  }
 else
  {
   alert('Geolocation unsupported');
  }
} 
$(document).ready(function(){initialize() });

再一次,这是一个非常简单的例子,它只是询问位置并发出一些警报。 奇怪的是,当我启动脚本时,我似乎无法得到任何答案。 有谁知道为什么? 在此先感谢:)

【问题讨论】:

  • 您使用的是哪个版本的 Firefox?
  • 这个例子也适用于 FF。也许您没有启用位置共享。
  • 在 2020 年仍然发生在我身上,使用 Firefox “开发者版” 76。真可惜。

标签: javascript jquery html


【解决方案1】:

实际上我在 Chrome 32(运行良好)和 Firefox 26(不走运)上测试了它。

我尝试在 Firefox 上启用地理定位,但仍然不起作用。

我浏览了谷歌上的几个网站,最后我理解的是 Firefox 26 和 27 在检测地理位置方面存在错误。

为了测试功能,您可以运行这个 LIVE DEMO,它是一个普通的 javascript。

initialize();

function displayLocation(position) {
    var cords = position.coords;
    alert("displayLocation, lat='"+cords.latitude+"'; long='"+cords.longitude+"'");
}

function displayError(positionErr) {
    alert("error");
}

function initialize() {
    if (!(navigator.geolocation == 'undefined')) {
        navigator.geolocation.getCurrentPosition(displayLocation, displayError, {
            timeout: 10000
        });
    } else {
        alert('Geolocation unsupported');
    }
}

【讨论】:

    【解决方案2】:

    看来笔记很重要:

    http://www.w3schools.com/html/html5_geolocation.asp

    浏览器支持

    Internet Explorer 9+、Firefox、Chrome、Safari 和 Opera 支持地理定位。

    注意:对于具有 GPS 的设备(如 iPhone),地理位置要准确得多。

    尝试一下: http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_geolocation

    -> 在使用 Firefox 26.0 的计算机上:什么也没发生。即使共享位置(未连接 GPS 设备)。

    -> 在使用 firefox 27.0 的 android 智能手机上:从 GPS 获取纬度和经度

    【讨论】:

      【解决方案3】:

      基于 Mozilla Firefox 24+ 的地理定位似乎存在问题

      https://support.mozilla.org/en-US/questions/984520

      以管理权限运行 Firefox(尤其是在 Windows 上)。

      【讨论】:

        猜你喜欢
        • 2019-11-29
        • 2016-09-08
        • 1970-01-01
        • 2013-09-29
        • 2012-11-27
        • 2012-02-27
        • 2017-02-08
        • 2014-08-23
        • 2013-07-12
        相关资源
        最近更新 更多