switchToLocationSettings()后如何检测gps是否开启?

How to detect whether the gps is enabled after switchToLocationSettings()?

我正在研究 ionic 项目。我需要根据当前地区获取项目。为了获取当前地区,我尝试了以下方法。

cordova.plugins.diagnostic.isLocationEnabled(function(enabled) {
 alert("Location is " + (enabled ? "enabled" : "disabled"));
 if(!enabled)
  cordova.plugins.diagnostic.switchToLocationSettings();
 var geocoder = new google.maps.Geocoder();
 if (navigator.geolocation) {
   navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
 } 
 function successFunction(position) {
  var lat = position.coords.latitude;
  var lng = position.coords.longitude;
 }
 function errorFunction(){
  $ionicPopup.alert({
  title:"Alert",
  content:"Geocoder failed"
 });
}
function codeLatLng(lat, lng) {
 geocoder.geocode({'latLng': new google.maps.LatLng(lat, lng)}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    if (results[1]) {
      for (var i=0; i<results[0].address_components.length; i++) {
        for (var b=0;b<results[0].address_components[i].types.length;b++) {
          if (results[0].address_components[i].types[b] == "administrative_area_level_2") {
               district= results[0].address_components[i];
          }
        }
      }
     alert(distrcit.long_name);
    }
    else {
      alert("No results");
    }
   }
   else {
    alert("Geocoder Failed");
   }
  });
 }
}, function(error) {
});

如果在切换到位置设置后启用了 GPS,则此代码可以正常工作。当 GPS 未启用并返回应用程序时,我遇到了问题。转到位置设置并返回应用程序后,如何检测它是否已启用?尽管我在 Whosebug 中参考了很多帖子,但我并没有得到我真正需要的解决方案。所以请帮我解决一下。

How can I detect whether it is enabled or not after moving to location settings and getting back to the app?

您可以使用 resume event.

检测您的应用何时返回前台
function onResume() {
    cordova.plugins.diagnostic.isLocationEnabled(function(enabled) {
        console.log("Location is " + (enabled ? "enabled" : "disabled"));           
        // etc.
    }, function(error) {});
}
document.addEventListener("resume", onResume, false);