使用此代码:
if (ionic.Platform.isIOS()) {
cordova.plugins.diagnostic.switchToSettings();
} else {
cordova.plugins.diagnostic.switchToLocationSettings();
}
我可以打开 native 设备配置来标记地理位置。但我不知道当用户打开地理定位时该怎么做,它只是执行该代码但没有从中得到答案。我想(如果可能的话)知道用户是否打开了地理定位。
我该怎么做?
我正在使用 ionic1
,尽管我认为此解决方案的操作适用于任何版本。非常感谢。
最佳答案
不幸的是,当用户从设备设置打开/关闭位置时,您无法获得回调或响应,因为没有这种方式与 native 设置进行通信。
但是有一个解决方法。使用 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 平台发生了变化/因此您无法打开设置>隐私>定位服务。
关于javascript - 获得激活地理定位结果的 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50804783/