【问题标题】:navigator.geolocation.getCurrentPosition always fail in chrome and firefoxnavigator.geolocation.getCurrentPosition 在 chrome 和 firefox 中总是失败
【发布时间】:2012-04-01 16:23:39
【问题描述】:

当我尝试测试我的 “navigator.geolocation.getCurrentPosition”网页。这是我的 测试结果及代码:

我的代码:

function detectLocation() 
{
  if (navigator.geolocation) 
  {
    navigator.geolocation.getCurrentPosition(geocodePosition, onError, { timeout: 30000 });
    navigator.geolocation.watchPosition(watchGeocodePosition);
  } 
  else 
  {
    onError();
  }
}

此函数在调用“body”onload 事件时运行。我曾尝试将超时更改为 10000 和 20000,但仍然得到相同的结果。我还允许 crome 和 Firefox 获取我的位置。

结果:

  1. 使用 chrome (v 17.0.963.79 m),结果总是出现 onError 调用 navigator.geolocation.getCurrentPosition 时的函数。
  2. 使用 Firefox (v 10.0.2),结果总是转到 onError 函数 当 navigator.geolocation.getCurrentPosition 被调用时。
  3. 使用 IE (v 9),结果非常棒,我得到了当前位置。

在这种奇怪的情况下谁能帮助我?我真的没有任何想法来解决这个问题,我急于我的项目截止日期。之前谢谢。

编辑: 这几天我取得了一些进展,错误代码代码为 2,并带有一条消息 “网络位置提供程序位于 'https://maps.googleapis.com/maps/api/browserlocation/json?browser=chromium&sensor=true ' : 响应格式错误”。还没解决,有大神知道怎么解决吗?

【问题讨论】:

  • 您的 if 语句没有任何真正的测试对我来说似乎很不寻常。我希望看到类似 if (navigator.geolocation!=null) 或类似的东西。
  • @Terry 看起来“if”语句没有问题,即使我将其更改为您的建议,这个奇怪的结果仍然存在。你知道为什么只有 IE 才能检索到正确的位置吗?实际上我是印度尼西亚人,我现在的职位在东爪哇。
  • 您可能会发现差异取决于您是在 LAN 还是 wifi 上,这会影响您的结果吗?
  • @user920962 这个结果使用了通过 LAN 的 DSL 线路,你能具体说说 wifi 是什么吗?据我所知,在我的国家 wifi 热点只使用了一条 DSL 线路,然后他们使用 wifi 路由器进行传播。如果它是您提到的 wifi,结果将是相同的,但是当您在foursquare 上查看时,它在所有浏览器中都能正常运行。使用navigator.geolocation有什么特殊设置或初始化吗?

标签: javascript html firefox google-chrome


【解决方案1】:

我模拟了这个问题,发现只有当 html 页面托管在 Web 服务器上时才会调用成功回调函数,而不是从文件系统打开时调用。

为了测试,我直接从 C: 驱动器打开了文件,但回调不起作用,然后将文件托管在 Internet 信息服务 (IIS) 上,回调确实起作用。

<html>
<body onload="detectLocation()">
<!-- This html must be hosted on a server for navigator.geolocation callbacks to work -->

<div id="status"></div>

<script type="text/javascript">
function detectLocation()
{
  log("detectLocation() starting");
  if (navigator.geolocation)
  {
    log("navigator.geolocation is supported");
    navigator.geolocation.getCurrentPosition(geocodePosition, onError, { timeout: 30000 });
    navigator.geolocation.watchPosition(watchGeocodePosition);
  }
  else
  {
    log("navigator.geolocation not supported");
  }
}
function geocodePosition(){
    log("geocodePosition() starting");
}

function watchGeocodePosition(){
    log("watchGeocodePosition() starting");
}

function onError(error){
    log("error " + error.code);
}
function log(msg){
    document.getElementById("status").innerHTML = new Date() + " :: " + msg + "<br/>" + document.getElementById("status").innerHTML;
}
</script>
</body>
</html>

【讨论】:

  • 我必须添加一件事,localhost 也不起作用。只有适当的公共托管才能与“navigator.geolocation”一起使用。此外,如果您的 ISP 未正确设置您的专用域服务器,则 navigator.geolocation 将无法正常工作。看起来这是客户端脚本,但它很大程度上取决于服务器或域设置。
  • 大多数主流浏览器现在要求网站使用 https:// 否则访问 getCurrentPosition 和 watchPosition 将被拒绝
【解决方案2】:

我也收到了这条消息:

消息:“‘https://www.googleapis.com/’上的网络位置提供商:返回错误代码 404。”,代码:2

我可以通过打开我的 wifi 适配器来解决它

【讨论】:

  • 确认!我正在使用蜂窝调制解调器在笔记本电脑上工作,并得到完全相同的 404 错误。重新打开我的 WiFi 适配器解决了该错误。奇数。
  • 也在这里工作过。正在远程进行一些开发并使用我的 iPhone 进行连接。通过iPhone连接时,得到404。找到一个本地wi-fi热点,连接,404消失了。回到系绳,404回来了。
【解决方案3】:

我有同样的问题。 Chrome 浏览器不会在 30000 毫秒超时后返回位置。 Firefox 也没有返回位置。我添加了选项 enableHighAccuracy 并将其设置为 false 但没有任何更改(false 是默认选项)。当我将其更改为 true 时,地理定位开始工作!
这是我的最终代码,

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(
            function(position) {
                // Get current cordinates.
                positionCords = {"lat": position.coords.latitude, "lng": position.coords.longitude};
            },
            function(error) {
                // On error code..
            },
            {timeout: 30000, enableHighAccuracy: true, maximumAge: 75000}
    );
}  

【讨论】:

  • 如此简单,但却是 2021 年以后的正确解决方案。接受的答案根本不起作用。
【解决方案4】:
【解决方案5】:

我知道这是老话题,但最近我也遇到了这个错误:

message: "Network location provider at 'https://www.googleapis.com/' : Returned error code 404.", code: 2

解决方法是获取谷歌地图的 api 密钥并在您的代码中使用它

<script src='https://maps.googleapis.com/maps/api/jscallback=initMap &signed_in=true&key=YOUR-API-KEY' async defer></script>

您可以在这里获取 API KEY:https://developers.google.com/maps/documentation/javascript/get-api-key#key

【讨论】:

  • 这对我不起作用。在某些情况下,我不断收到错误消息"Network location provider at 'https://www.googleapis.com/' : Returned error code 404."。我猜你很幸运,这是一个巧合。有几次错误是400 而不是404
【解决方案6】:

我有同样的问题,解决方案是增加超时时间,因为移动网络比有线网络慢

 {timeout: 30000, enableHighAccuracy: true, maximumAge: 75000}

以及启用蜂窝定位

在设备设置中打开“Wifi 和蜂窝定位”选项。

【讨论】:

  • 需要放在哪里?
【解决方案7】:

这将打印您所在位置的纬度和经度

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
  <script>
    function getLocation() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
        } else { 
            alert("Geolocation is not supported by this browser.");
        }

    }
    function showPosition(position) {
        document.getElementById('idLatitude').value = position.coords.latitude;
        document.getElementById('idLongitude').value = position.coords.longitude;

    }
    </script>

</head>
<body onload="getLocation()">
<form action="HelloWorld" method="post">
    <input id="idLatitude"  type="text" name="strLatitude">
    <input id="idLongitude" type="text" name="strLongitude">

</form>
</body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-06
    • 2016-10-14
    • 1970-01-01
    • 2015-07-02
    • 1970-01-01
    • 2012-02-05
    相关资源
    最近更新 更多