1

I must be doing something wrong here, but I can't find what... I'm making a page for my company website that, among other things, provides directions to job sites based on the user's current location. To get their location, I'm using the Geolocation API as implemented in Firefox (I was testing with FF9 and now FF10). For the moment I'm testing with the sample code that is in the Wikipedia article:

<html>
<head>
<title>Geolocation Test</title>
<script type='text/javascript'>
var gl;

function displayPosition(position) {
  var p = document.getElementById("p");
  p.innerHTML = "<table border='1'><tr><th>Timestamp</th><td>" + position.timestamp +
    "<tr><th>Latitude (WGS84)</th><td>" + position.coords.latitude + " deg</td></tr>" +
    "<tr><th>Longitude (WGS84)</th><td>" + position.coords.longitude + " deg</td></tr></table>";
}

function displayError(positionError) {
  alert("error " + positionError.code);
}

try {
  if (typeof navigator.geolocation === 'undefined'){
    gl = google.gears.factory.create('beta.geolocation');
  } else {
    gl = navigator.geolocation;
  }
} catch(e) {}

if (gl) {
  gl.getCurrentPosition(displayPosition, displayError);
} else {
  alert("Geolocation services are not supported by your web browser.");
}
</script>

<body>
<p id='p'></p>
</body>
</html>

(copy/paste this into a text editor, save as "whatever.html", and drag the file to the browser to open it)

Very simple code, and it actually worked fine last Thursday, 01/26/12. (I know, I know, people say "It worked yesterday!" when they actually goofed something up, but I swear it really did.) Since then, however, it's been giving me error code 2, "POSITION_UNAVAILABLE". I have tried it with my work computer on my work network, my work and home computers on my home internet connection, and my work computer on a wifi hotspot from my phone, and it gives error code 2 every time.

I thought maybe I'd somehow goofed something up in my browser or my network settings or something (though I couldn't imagine what), so I tried the next test, which everyone should be able to do:

  1. Go to maps.google.com.
  2. Search for "seattle" (or some other location, whatever).
  3. Click the "Get Directions" button.
  4. In the directions boxes on the left, "A" should say "My location" and "B" should say "seattle". (If it doesn't say "My Location" in A, type "m" into the box, and the auto-populating dropdown will usually have "My Location" as the first option.)
  5. Click the "GET DIRECTIONS" button.
  6. Firefox should pop up a message saying maps.google.com wants to know your location. Click "Share Location".
  7. After a couple seconds, it should either show you directions from somewhere (wherever it thinks your server is) to Seattle, or the "A" box should go blank and it'll say "Could not find your location" or similar toward the top of the map.

And here's the weird thing: I have never yet gotten this to work on any computer. My work computer (on both FF10 and Chrome), my coworkers' computers, my home computer, and my GF's work computer all do the same thing; none can find the location.

So my question is: Is the location API down or something, or am I screwing something up? I haven't been able to find any indications online of an API outage, and it seems like an outage is very unlikely, but if so, why does it not work on any computer I've tried it on? Can anyone else duplicate these findings or know what I'm doing wrong?

Thanks!

4

2 回答 2

1

您的脚本工作正常,谷歌地图中的位置功能也是如此(FF9.0.1)

你在哪里?(我的意思是物理上)如果您在某个偏远的地方,Google 可能还无法计算您的位置!在一些大城市尝试一下,也许......

另一种可能性是您无权访问 Google 的位置服务器,就好像您的 ISP 阻止了它一样……

于 2012-02-02T00:24:27.213 回答
0

您的脚本似乎在 Chrome 中运行,但在 FF 中没有。我在 FF 中遇到的错误是位置不可用,这意味着 FF 的定位服务可能无法访问我的 wifi 网络。我假设您收到的错误是警报中的错误 2?

如果您使用 Firebug,您可以在第 15 行设置一个停止点。当它停止时,单击右侧 Watch 面板中的 positionError 并检查代码是什么。我的阅读:代码 2 转换为不可用。

我建议你有一些其他的代码来处理这个实例。

于 2012-02-02T00:52:32.937 回答