Get promise of the result of activating geolocation

◇◆丶佛笑我妖孽 提交于 2019-12-08 03:16:57

问题


with this code:

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

I can open the native device configuration to mark the geolocation. but I do not know how to do it when the user turns on the geolocation, it simply executes that code but does not get an answer from that. I would like (if possible) to know if the user turned on the geolocation or not.

how can I do it?

I am using ionic1, although I suppose that the operation for this solution would apply to any version. thank you very much.


回答1:


Unfortunately you can not get callback or response when user turn on/off location from device setting because there is no such way to communicate with Native settings.

But there is a workaround for this. Use isLocationEnabled() of cordova-diagnostic-plugin.

Below is complete working code:

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

Note: To open location setting (Settings > Privacy > Location Services) on iOS there is plugin but There is change/ in iOS 11 platform due to which you can not open Settings > Privacy > Location Services.



来源:https://stackoverflow.com/questions/50804783/get-promise-of-the-result-of-activating-geolocation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!