Last active
June 22, 2020 15:02
-
-
Save NielsLeenheer/ac243873b1fe06fcb26c to your computer and use it in GitHub Desktop.
Make navigator.geolocation.getCurrentPosition more reliable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Niels,
you can put this 2 lines at line 17:
success=(success || function () {});
failure=(failure || function () {});
to check if they are undefined.