Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When GPS permission is denied, poll for permission changes #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions packages/mdg:geolocation/geolocation.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// https://developer.mozilla.org/en-US/docs/Web/API/PositionError
var PERMISSION_DENIED = 1;

// is location refreshing currently on?
var watchingPosition = false;

Expand All @@ -14,18 +17,39 @@ var options = {
timeout: 10000
};

var polling = false;
var currentWatch = null;

// calling watchPosition checks to see whether you now have gps permissions
var checkForPermissionChanges = function () {
navigator.geolocation.clearWatch(currentWatch);
currentWatch = navigator.geolocation.watchPosition(onPosition, onError, options);

Meteor.setTimeout(function () {
if (polling) {
checkForPermissionChanges();
}
}, 10000);
};

var onError = function (newError) {
error.set(newError);

if (newError.code === PERMISSION_DENIED && ! polling) {
polling = true;
checkForPermissionChanges();
}
};

var onPosition = function (newLocation) {
location.set(newLocation);
error.set(null);
polling = false;
};

var startWatchingPosition = function () {
if (! watchingPosition && navigator.geolocation) {
navigator.geolocation.watchPosition(onPosition, onError, options);
currentWatch = navigator.geolocation.watchPosition(onPosition, onError, options);
watchingPosition = true;
}
};
Expand Down Expand Up @@ -59,7 +83,7 @@ Geolocation = {
return location.get();
},
// simple version of location; just lat and lng

/**
* @summary Get the current latitude and longitude
* @return {Object | null} An object with `lat` and `lng` properties,
Expand Down