获得激活地理定位结果的承诺

Get promise of the result of activating geolocation

使用此代码:

if (ionic.Platform.isIOS()) {
    cordova.plugins.diagnostic.switchToSettings();
} else {
cordova.plugins.diagnostic.switchToLocationSettings();
}

我可以打开本机设备配置来标记地理位置。但我不知道当用户打开地理定位时该怎么做,它只是执行该代码但没有从中得到答案。我想(如果可能的话)知道用户是否打开了地理定位。

我该怎么做?

我正在使用 ionic1,尽管我认为此解决方案的操作适用于任何版本。非常感谢。

很遗憾,当用户从设备设置中转到 on/off 位置时,您无法获得回调或响应,因为没有这种方式可以与本机设置进行通信。

但是有一个解决方法。使用 isLocationEnabled() of cordova-diagnostic-plugin

下面是完整的工作代码:

  $scope.$on("$ionicView.enter", function() {
        cordova.plugins.diagnostic.isLocationEnabled(function(enabled) {
          console.log("Location setting is " + (enabled ? "enabled" : "disabled"));

          if (!enabled) {
            var templateMsg = "Location is not enabled!\nDo you want to enable location service?"
            var confirmPopup = $ionicPopup.confirm({
              title: '<b>Location Service</b>',
              template: '<input alert-enter-key style="position: absolute; left: -9999px;">' + templateMsg.replace(/\n/g, '<br>'),
              okText: "Enable",
              okType: 'ok-button',
              cancelText: "Not now",
              cancelType: 'cancel-button'
            });

            confirmPopup.then(function(res) {
              if (res) {
                if (ionic.Platform.isIOS()) {
                  if (window.cordova && window.cordova.plugins.settings) {
                    window.cordova.plugins.settings.open("settings", function() {
                        console.log('settings opened');
                      },
                      function() {
                        console.log('failed to open settings');
                      }
                    );
                  } else {
                    console.log('openNativeSettingsTest is not active!');
                  }
                } else {
                  cordova.plugins.diagnostic.switchToLocationSettings();
                }                  }
            });
          }
        }, function(error) {
          console.error("The following error occurred: " + error);
        });
      });

注意:要在 iOS 上打开位置设置(设置 > 隐私 > 定位服务),plugin 但 iOS 11 平台中存在变化/,因此 您无法 打开 设置 > 隐私 > 定位服务