From 7a133a7e823100ff6aabe19aed3368ce3e66b621 Mon Sep 17 00:00:00 2001 From: Loren Sands-Ramshaw Date: Tue, 29 Sep 2015 19:35:46 -0400 Subject: [PATCH] When GPS permission is denied, poll for permission changes --- packages/mdg:geolocation/geolocation.js | 28 +++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/packages/mdg:geolocation/geolocation.js b/packages/mdg:geolocation/geolocation.js index 1c5f008..603de9d 100644 --- a/packages/mdg:geolocation/geolocation.js +++ b/packages/mdg:geolocation/geolocation.js @@ -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; @@ -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; } }; @@ -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,