Html Safari 5 中的地理定位

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3791442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 04:37:51  来源:igfitidea点击:

Geolocation in Safari 5

htmlsafarigeolocation

提问by Sunil Shenoy

I have a application which reports my location using HTML5 geolocation. The application works correct on Firefox and Chrome, but on Safari 5, it says that Safari does not support Geolocation.

我有一个应用程序,它使用 HTML5 地理定位报告我的位置。该应用程序在 Firefox 和 Chrome 上工作正常,但在 Safari 5 上,它表示 Safari 不支持地理定位。

From what I read, Safari 5 does support Geolocation. What am I missing?

据我所知,Safari 5 确实支持地理定位。我错过了什么?

Thanks for your time.

谢谢你的时间。

Sunil

苏尼尔

回答by andyvanee

Looks like Safari geolocation only works when connected with wifi. When connected with a wired connection Safari calls the error callback from the geolocation functions.

看起来 Safari 地理定位仅在与 wifi 连接时才有效。当与有线连接连接时,Safari 会从地理定位函数调用错误回调。

To test this, try this in the web console:

要对此进行测试,请在 Web 控制台中尝试此操作:

navigator.geolocation.getCurrentPosition(
  function(){console.log("success")},
  function(){console.log("error")}
);

With Safari/wifi this returns 'success' after a second or two, but on a wired connection it returns 'error' immediately.

对于 Safari/wifi,它会在一两秒后返回“成功”,但在有线连接上它会立即返回“错误”。

( using Safari 5.1 - 8.x / Mac OSX 10.7 - 10.10 )

(使用 Safari 5.1 - 8.x / Mac OSX 10.7 - 10.10)

回答by npdoty

Although nominallygeolocation support in Safari 5 is available on both Mac and Windows, I'm hearing of more issues on the Windows side.

尽管Safari 5 中名义上的地理定位支持在 Mac 和 Windows 上都可用,但我听说 Windows 方面存在更多问题。

For example, see this similar StackOverflow question. In that case, though, navigator.geolocationwas available, it just never received a successful callback. When you say that "it says that Safari does not support Geolocation", who is saying that to you? Are you getting an error callback, is navigator.geolocationnull, or have you just read this elsewhere (and if so, where?)?

例如,请参阅此类似的StackOverflow 问题。但是,在那种情况下,navigator.geolocation它是可用的,只是从未收到成功的回调。当您说“它说 Safari 不支持地理定位”时,是谁在对您说?您是否收到错误回调,为navigator.geolocation空,或者您只是在其他地方(如果是,在哪里?)

回答by zpesk

hmmm, I'm a little stumped. Safari 5 does support geolocation through HTML 5. You might want to try to use an HTML 5 feature detection service like Modernizr. This will tell you what browsers support html5 and css3 standards. I'm using Safari 5 and Modernizr shows that the geolocation API is supported.

嗯,我有点难住了。Safari 5 确实通过 HTML 5 支持地理定位。您可能想尝试使用像Modernizr这样的 HTML 5 特征检测服务。这将告诉您哪些浏览器支持 html5 和 css3 标准。我使用的是 Safari 5,Modernizr 显示支持地理定位 API。

回答by morgan_il

How do you fetch Google Maps API script ? Is sensor param set to true or false ? I had Safari Geolocation not working (under Windows) until I changed "sensor=false" to "sensor=true" like the example below:

您如何获取 Google Maps API 脚本?传感器参数是否设置为 true 或 false ?在我将“sensor=false”更改为“sensor=true”之前,Safari Geolocation 无法正常工作(在 Windows 下),如下例所示:

<script src="http://maps.googleapis.com/maps/api/js?sensor=true" type="text/javascript"></script>

And it works perfectly in every browser : IE9, Chrome, FF10+, Safari (Win).

它在所有浏览器中都能完美运行:IE9、Chrome、FF10+、Safari (Win)。

Noticed another strange thing- it works only with WiFi in Safari - if you wired-connected, it won't work and would just stuck trying forever.

注意到另一件奇怪的事情——它只适用于 Safari 中的 WiFi——如果你有线连接,它将无法工作,并且会一直尝试下去。

So, to fix Safari just add the following { maximumAge: 600000, timeout: 10000 }to handle timeout :

因此,要修复 Safari,只需添加以下内容{ maximumAge: 600000, timeout: 10000 }来处理超时:

// Try W3C Geolocation (Preferred)
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
        handleGeolocationAcquired(position.coords.latitude, position.coords.longitude);
    }, function(error) {
        handleNoGeolocation();
    },
        { maximumAge: 600000, timeout: 10000 });

    // Try Google Gears Geolocation
} else if (google.gears) {
    var geo = google.gears.factory.create('beta.geolocation');
    geo.getCurrentPosition(function (position) {
        handleGeolocationAcquired(position.latitude, position.longitude);
    }, function () {
        handleNoGeolocation();
    });
}
//Cannot obtain Geo Location
else {
    handleNoGeolocation();
}

This way, in case you're in Safari (under Windows) and wired-connected (=> endless loop acquiring Geo location) : after 10 seconds, it will fallback to error handler.
BTW - maximumAgeparam just sets location expiration.

这样,如果您使用 Safari(在 Windows 下)和有线连接(=> 无限循环获取地理位置):10 秒后,它将回退到错误处理程序。
顺便说一句 - maximumAge参数只是设置位置过期。

回答by Igor Kanshyn

Calling getCurrentPosition(...)without specified timeout makes Safari stuck there for minutes.

getCurrentPosition(...)没有指定超时的情况下调用会使 Safari 卡在那里几分钟。