Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Geolocation in Safari 5

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.

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

Thanks for your time.

Sunil

like image 895
Sunil Shenoy Avatar asked Sep 24 '10 22:09

Sunil Shenoy


2 Answers

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.

like image 104
morgan_il Avatar answered Sep 28 '22 15:09

morgan_il


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 )

like image 23
andyvanee Avatar answered Sep 28 '22 17:09

andyvanee