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
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.
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 )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With