admin管理员组文章数量:1402939
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.
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.
Share Improve this question edited Mar 14, 2016 at 2:40 dovetalk 1,9901 gold badge13 silver badges21 bronze badges asked Mar 14, 2016 at 2:20 Lewis M HackfathLewis M Hackfath 1316 silver badges17 bronze badges 1- This Doesn't meet the requirements of the project as not all our staff are onsite we are using sub contractors as well. – Lewis M Hackfath Commented Mar 14, 2016 at 2:45
3 Answers
Reset to default 4I 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.
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.
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]
)
本文标签: javascriptForce GeoLocation to allowStack Overflow
版权声明:本文标题:javascript - Force GeoLocation to allow - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744331535a2600988.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论