Preetham Reddy
Preetham Reddy

Reputation: 611

Ask for location access after initial rejection in PhoneGap apps

I'm developing cross platform applications. I'm able to request access to location using the Phone Gap API. If the user clicks okay, then I'm able to get the latitude and longitude using PhoneGap API and send it to the server.

However, I'm facing issues if the user clicks "Don't Allow" initially. After that, if he tries to refresh page, I want the device to show the popup again to request access to the location.

How do we do that in Single Page Applications?

    var loadPanelMessage = ko.observable("Sipping..."),
    loadPanelVisible = ko.observable(false),
    lat = ko.observable(''),
    lon = ko.observable('');
navigator.geolocation.getCurrentPosition(onSuccess, onError);

    var onSuccess = function (position) {
    lat(position.coords.latitude);
    lon(position.coords.longitude);
    timestamp(position.timestamp);
};

function onError(error) {
    alert('code: ' + error.code + '\n' +
          'message: ' + error.message + '\n');
//How should I handle the error, so that it asks for Geolocation again?
}

Upvotes: 0

Views: 911

Answers (2)

AlexLopezIT
AlexLopezIT

Reputation: 141

Based on the cordova plugins here org.apache.cordova.geolocation

All you need to do is a watchPosition to check the PositionError, and use also the cordova-plugin-dialogs to show the native notification

navigator.geolocation.watchPosition((function(_this) {
  return function(position) {
    // Do something here
};
})(this), function(error) {
  var errorButton, errorMsg, errorTitle;
  errorTitle = "Location Services";
  errorButton = "Ok";
  if (error.code === 1) {
    errorMsg = "\"AppName\" needs access to your location. Please turn on Location Services in your device settings.";
  }
  if (error.code === 2) {
    errorMsg = "This device is unable to retrieve a position. Make sure you are connected to a network";
  }
  if (error.code === 3) {
    errorMsg = "This device is unable to retrieve a position. Make sure you have Location Services enabled for \"AppName\"";
  }
  if (error.code === 1 || error.code === 2 || error.code === 3) {
    return navigator.notification.alert(errorMsg, errorDismissed(), errorTitle, errorButton);
  }
}, {
  enableHighAccuracy: true,
  maximumAge: 20000,
  timeout: 10000
});

Upvotes: 0

mentat
mentat

Reputation: 2768

You cannot reshow that popup, it is shown by iOS and once user click's "Don't Allow" you cannot force that popup. All you can do is to let your user know about the situation and direct the user to iOS settings to enable location services for your app.

Here is a nice read about the problem: https://medium.com/on-startups/96fa4eb54f2c

Upvotes: 2

Related Questions