Geolocation in Safari 5

后端 未结 5 1166
别那么骄傲
别那么骄傲 2020-12-06 05:43

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 su

5条回答
  •  佛祖请我去吃肉
    2020-12-06 06:18

    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:

    
    

    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.

提交回复
热议问题