Skip to content

Instantly share code, notes, and snippets.

@mattbontrager
Created January 19, 2013 02:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattbontrager/4570482 to your computer and use it in GitHub Desktop.
Save mattbontrager/4570482 to your computer and use it in GitHub Desktop.
Working navigator.gelocation for WebApp (navigator.standalone) in iOS 6.
if (window.navigator.geolocation) {
var accuracyThreshold = 100,
timeout = 10 * 1000,
watchID = navigator.geolocation.watchPosition(function(position) {
$('#latitude').val(position.coords.latitude); // set your latitude value here
$('#longitude').val(position.coords.longitude); // set your longitude value here
// if the returned distance accuracy is less than your pre-defined accuracy threshold,
// then clear the timeout below and also clear the watchPosition to prevent it from running continuously
position.coords.accuracy < accuracyThreshold && (clearTimeout(delayClear), navigator.geolocation.clearWatch(watchID))
}, function(error) {
// if it fails to get the return object (position), clear the timeout
// and cancel the watchPosition() to prevent it from running continuously
clearTimeout(delayClear);
navigator.geolocation.clearWatch(watchID);
// make the error message more human-readable friendly
var errMsg;
switch (error.code) {
case '0':
errMsg = 'Unknown Error';
break;
case '1':
errMsg = 'Location permission denied by user.';
break;
case '2':
errMsg = 'Position is not available';
break;
case '3':
errMsg = 'Request timeout';
break;
}
}, {
enableHighAccuracy: true,
timeout: timeout,
maximumAge: 0
}),
delayClear = setTimeout(function() {
navigator.geolocation.clearWatch(watchID);
}, timeout + 1E3); // make this setTimeout delay one second longer than your watchPosition() timeout
}
else {
throw new Error("Geolocation is not supported.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment