Skip to content

Instantly share code, notes, and snippets.

@NielsLeenheer
Last active June 22, 2020 15:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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);
}
}
})();
@NielsLeenheer
Copy link
Author

This script works around three common problems with the getCurrentPosition() function on Smart TV's and consoles.

  1. Neither the failure nor the success callback is called
  2. The success callback is called with the coordinates 0,0
  3. The success callback is called with the coordinates of Google's HQ in Mountain View, CA

@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