【问题标题】:Get promise of the result of activating geolocation获得激活地理定位结果的承诺
【发布时间】:2018-11-21 02:53:21
【问题描述】:

使用此代码:

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

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

我该怎么做?

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

【问题讨论】:

    标签: javascript ionic-framework ionic2 geolocation ionic3


    【解决方案1】:

    很遗憾,当用户从设备设置中打开/关闭位置时,您无法获得回调或响应,因为没有这种方式与原生设置进行通信。

    但是有一个解决方法。使用isLocationEnabled()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 平台中有更改/因此您不能打开设置>隐私>定位服务

    【讨论】:

    • 非常感谢,这在 android 中运行良好,但在 ios 中不需要我直接打开地理定位,可以为此做些什么?对了,我用的不是 $scope.$on("$ionicView.enter" => addEventListener("resume") 一样吗?
    • @yavg 我已经更新了在 iOS 中打开位置设置的答案。看看吧!
    • 我正在尝试虚拟机 iphone7 和 iphone 8,我必须使用“cordova.plugins.diagnostic.switchToLocationSettings();”
    • 我不确定,但它总是通过 else 'openNativeSettingsTest is not active!'
    • cordova.plugins.diagnostic.switchToLocationSettings(); 不支持 iOS。这就是为什么如果设备是 iOS,我添加了window.cordova.plugins.settings.open("locations"。使用我上面添加的内容。它将工作 100%。
    猜你喜欢
    • 2017-11-29
    • 2019-04-19
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 2017-10-18
    • 1970-01-01
    • 2019-02-10
    • 2016-01-24
    相关资源
    最近更新 更多