获得激活地理定位结果的承诺 [英] Get promise of the result of activating geolocation
问题描述
使用此代码:
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.
我该怎么办?
我正在使用 ionic1
,虽然我认为此解决方案的操作适用于任何版本。非常感谢。
I am using ionic1
, although I suppose that the operation for this solution would apply to any version. thank you very much.
推荐答案
不幸的是,当用户从设备设置打开/关闭位置时,您无法获得回调或响应,因为那里没有这种方式与Native设置进行通信。
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.
但是有一个解决方法。使用 isLocationEnabled()。 com / dpa99c / cordova-diagnostic-pluginrel =nofollow noreferrer> cordova-diagnostic-plugin 。
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);
});
});
注意:打开位置设置(设置>隐私>位置服务)在iOS上有插件,但有变化/在iOS 11平台中你不能打开设置>隐私>位置服务。
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.
这篇关于获得激活地理定位结果的承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!