Search code examples
javascripthtmlgeolocation

Force GeoLocation to allow


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:

  • Force Location Services to be on or Prevent the page from rendering if they are off
  • Bring up the allow/deny location services box on page load if they have previously denied their use.

Any help would be greatly appreciated.


Solution

  • 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:

    • Only render if the success handler is called and show the error if not (after timeout)
    • Redirect to an error page if the error handler is called

    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.