Reputation: 131
I am currently trying to build an app for employees to clock on and off from work each day.
I have currently setup geolocation to automatically get the location of the user and store it in a database however for obvious reasons if a user presses deny it cannot get the location.
Is there a way I can force location to be on for my page or prevent the page from loading if location is disabled.
I have managed to set this up with a function but if the user presses a not now button or select later the page still renders correctly and does not prevent the employee using the system.
What I need to do are the following:
Any help would be greatly appreciated.
Upvotes: 1
Views: 7052
Reputation: 332
The format for geolocation is
navigator.geolocation.getCurrentPosition(success, error, options)
You can check if user prevented the location prompt by following way
navigator.geolocation.getCurrentPosition(
function(position) {
// Success goes here
}.bind(this),
function(error) {
toastr.error("Please turn on location for this feature")
setTimeout(function() {
window.location.href = '/'
}, 2000)
}, [your options]
)
Upvotes: 1
Reputation: 8888
Once user denies, you can not get the lat/lng. This is enforced by the browsers. Another option is to get the IP address of the user as the fallback to map it to the physical address. But do take note, this approach works better when user is connecting to wifi. The accuracy for 3G/4G is at zip code level.
Upvotes: 1
Reputation: 1991
I don't know how you're generating your page, but if you can rely on Javascript to render, or if redirecting to an error page in case of a denial is acceptable, you can use rely on the fact that you'll get an error callback in case of a denial:
navigator.geolocation.getCurrentPosition(success[, error[, options]])
So you site could either:
success
handler is called and show the error if not (after timeout)Now, to your "clear denial", I doubt this is possible. The best I can suggest is that you have instructions on clearing the geo settings on the error page. These will depend on OS and browser, unfortunately.
Upvotes: 4