在PhoneGap应用程序中初次拒绝后请求访问位置

时间:2014-08-27 07:04:13

标签: ios cordova geolocation single-page-application

我正在开发跨平台应用程序。我可以使用Phone Gap API请求访问位置。如果用户点击正常,那么我可以使用PhoneGap API获取经度和纬度并将其发送到服务器。

但是,如果用户点击了“不要允许"原来。之后,如果他尝试刷新页面,我希望设备再次显示弹出窗口以请求访问该位置。

我们如何在单页应用程序中这样做?

    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?
}

2 个答案:

答案 0 :(得分:2)

您无法重新显示该弹出窗口,它会在iOS上显示,并且一旦用户点击“不允许”#34;你不能强迫弹出窗口。您所能做的就是让您的用户了解情况并将用户引导至iOS设置,以便为您的应用启用位置服务。

以下是关于此问题的精彩内容: https://medium.com/on-startups/96fa4eb54f2c

答案 1 :(得分:0)

基于此处的cordova插件org.apache.cordova.geolocation

您需要做的就是使用watchPosition检查 PositionError ,并使用cordova-plugin-dialogs来显示原生通知

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
});