Geolocation in Safari 5

久未见 提交于 2019-11-27 22:58:07

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.

To test this, try this in the web console:

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.

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

npdoty

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

For example, see this similar StackOverflow question. In that case, though, navigator.geolocation was 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.geolocation null, or have you just read this elsewhere (and if so, where?)?

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.

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:

<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).

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.

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

// 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 - maximumAge param just sets location expiration.

Igor Kanshyn

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!