simple question.. how do I set a redirect if the user selects not allow for the geolocator pop up? If allow and error I have covered. works fine. But I’ not sure how to set the not allow selection.
function showLocation(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
//window.location = "/restaurant/latitude/40.714269/longitude/-74.005972/";
window.location = "/restaurant/";
}
function errorHandler(err) {
if(err.code == 1) {
alert("Error: Can not access!");
}else if( err.code == 2) {
alert("Error: Position is unavailable!");
}
}
function getLocation(){
if(navigator.geolocation){
// timeout at 60000 milliseconds (60 seconds)
var options = {timeout:60000};
navigator.geolocation.getCurrentPosition(showLocation, errorHandler, options);
}else{
alert("Sorry, browser does not support geolocation!");
}
}
According to the W3C Geolocation API spec, the error handler should be invoked with the appropriate reason for the failure. This includes permission denied by the user.
Unfortunately, it seems like there is a bug/feature in Firefox where if you select
Not Nowfrom the popup, nothing happens – the error handler is not called. However, if you selectNever Share, your error handler does get called and with the code you posted, you’ll see the alert sayingError: Can not access!.There is no third option in Chrome (only
AllowandDeny) and things seem to work just fine there.Also see How to detect negative user response for geolocation where a solution is proposed (it involves using a timeout to detect that nothing has happened and treating it the same as permission denied)