Skip to content

Instantly share code, notes, and snippets.

@NielsLeenheer
Last active June 22, 2020 15:02
Show Gist options
  • Save NielsLeenheer/ac243873b1fe06fcb26c to your computer and use it in GitHub Desktop.
Save NielsLeenheer/ac243873b1fe06fcb26c to your computer and use it in GitHub Desktop.
Make navigator.geolocation.getCurrentPosition more reliable
(function() {
if (navigator.geolocation) {
function PositionError(code, message) {
this.code = code;
this.message = message;
}
PositionError.PERMISSION_DENIED = 1;
PositionError.POSITION_UNAVAILABLE = 2;
PositionError.TIMEOUT = 3;
PositionError.prototype = new Error();
navigator.geolocation._getCurrentPosition = navigator.geolocation.getCurrentPosition;
navigator.geolocation.getCurrentPosition = function(success, failure, options) {
var successHandler = function(position) {
if ((position.coords.latitude == 0 && position.coords.longitude == 0) ||
(position.coords.latitude == 37.38600158691406 && position.coords.longitude == -122.08200073242188))
return failureHandler(new PositionError(PositionError.POSITION_UNAVAILABLE, 'Position unavailable'));
failureHandler = function() {};
success(position);
}
var failureHandler = function(error) {
failureHandler = function() {};
failure(error);
}
navigator.geolocation._getCurrentPosition(successHandler, failureHandler, options);
window.setTimeout(function() { failureHandler(new PositionError(PositionError.TIMEOUT, 'Timed out')) }, 10000);
}
}
})();
@fantonangeli
Copy link

Hi Niels,
you can put this 2 lines at line 17:
success=(success || function () {});
failure=(failure || function () {});
to check if they are undefined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment