20

我正在使用 PhoneGap 构建一个应用程序。在使用 phonegap 的 Geolocation API 时,我意识到 API 超时有两个原因并抛出相同的错误: 1. 如果用户设备上未启用 GPS 2. 如果 GPS 已启用但无法获取位置用户(可能是多种原因,GPS 故障,天气不明等)

我在区分原因时遇到问题?关于如何做的任何想法?

我想知道是否有任何方法,我可以使用 Phonegap 检查用户设备上的 GPS 是否处于活动状态,以便我可以单独检查并将用户引导到通常放置 GEO 设置的设置窗口。不知道该怎么做?一个自定义的phonegap插件可能是?

4

6 回答 6

37

您可以在调用getCurrentPosition时检查geolocationError的PositionError参数中的错误代码。我猜当 gps 未启用时它将是 PositionError.PERMISSION_DENIED,而当 gps 启用时它将是 PositionError.POSITION_UNAVAILABLE 或 PositionError.TIMEOUT 但还有其他问题。

请注意,这取决于平台。您可能不得不编写一条人为的错误消息,上面写着“无法获取当前位置。GPS 信号较弱或 GPS 已关闭”。

您可以尝试的一件事是调用 getCurrentPosition 时的超时非常小,比如 1 毫秒。如果它说权限被拒绝,您可以断定 gps 已禁用,如果超时,您可以假定 gps 已启用。我没有时间对此进行测试,您可以根据测试结果编辑此答案。

您可以尝试的另一件事是使用android 的诊断 phonegap 插件。您必须确保您也使用其他平台的插件,但它们也大多都在那里。

于 2012-10-22T07:21:29.950 回答
6

为了最优雅地处理这种情况,您可以使用cordova.plugins.diagnostic检查 GPS 设置是否启用并(在 Android 6+ 上)检查应用程序是否具有运行时授权,以及(如果未启用)使用cordova-plugin-request-location-accuracy通过本机自动打开 GPS对话框,而不需要用户通过设置页面手动打开它。但是,由于后者依赖于设备上最新的 Google Play 服务库,因此如果自动切换失败,最好回退到手动切换。

示例应用演示

首先将所需的插件添加到您的项目中:

cordova plugin add cordova.plugins.diagnostic --save
cordova plugin cordova-plugin-request-location-accuracy --save

然后你会这样做:

function checkAvailability(){
    cordova.plugins.diagnostic.isGpsLocationAvailable(function(available){
        console.log("GPS location is " + (available ? "available" : "not available"));
        if(!available){
           checkAuthorization();
        }else{
            console.log("GPS location is ready to use");
        }
    }, function(error){
        console.error("The following error occurred: "+error);
    });
}

function checkAuthorization(){
    cordova.plugins.diagnostic.isLocationAuthorized(function(authorized){
        console.log("Location is " + (authorized ? "authorized" : "unauthorized"));
        if(authorized){
            checkDeviceSetting();
        }else{
            cordova.plugins.diagnostic.requestLocationAuthorization(function(status){
                switch(status){
                    case cordova.plugins.diagnostic.permissionStatus.GRANTED:
                        console.log("Permission granted");
                        checkDeviceSetting();
                        break;
                    case cordova.plugins.diagnostic.permissionStatus.DENIED:
                        console.log("Permission denied");
                        // User denied permission
                        break;
                    case cordova.plugins.diagnostic.permissionStatus.DENIED_ALWAYS:
                        console.log("Permission permanently denied");
                        // User denied permission permanently
                        break;
                }
            }, function(error){
                console.error(error);
            });
        }
    }, function(error){
        console.error("The following error occurred: "+error);
    });
}

function checkDeviceSetting(){
    cordova.plugins.diagnostic.isGpsLocationEnabled(function(enabled){
        console.log("GPS location setting is " + (enabled ? "enabled" : "disabled"));
        if(!enabled){
            cordova.plugins.locationAccuracy.request(function (success){
                console.log("Successfully requested high accuracy location mode: "+success.message);
            }, function onRequestFailure(error){
                console.error("Accuracy request failed: error code="+error.code+"; error message="+error.message);
                if(error.code !== cordova.plugins.locationAccuracy.ERROR_USER_DISAGREED){
                    if(confirm("Failed to automatically set Location Mode to 'High Accuracy'. Would you like to switch to the Location Settings page and do this manually?")){
                        cordova.plugins.diagnostic.switchToLocationSettings();
                    }
                }
            }, cordova.plugins.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY);
        }
    }, function(error){
        console.error("The following error occurred: "+error);
    });
}

checkAvailability(); // start the check
于 2016-07-16T10:42:26.407 回答
3

我在某些设备上遇到了类似的问题。设法用下面的代码解决它:

var options = {maximumAge: 0, timeout: 10000, enableHighAccuracy:true};
navigator.geolocation.getCurrentPosition(onSuccess, onError, options);
于 2013-10-28T16:54:49.827 回答
1

我在上面尝试了 DaveAlden 的解决方案,但它不起作用,部分原因是他调用的第一个函数是每个插件文档cordova.plugins.diagnostic.isGpsLocationAvailable()的 Android-only 。

正如他所说,首先添加两个插件:

cordova plugin add cordova.plugins.diagnostic --save
cordova plugin cordova-plugin-request-location-accuracy --save

然后添加以下函数:

// Checks to see if GPS is enabled AND if the app is authorized
checkEnabled(){
    cordova.plugins.diagnostic.isLocationAvailable(
      (available) => { onSuccess(available); },
      (error) => { goToSettings(error); }
    );
}

onSuccess(available) {
  if(available) { // do something };
  else goToSettings(available);
}

// Output error to console
// Prompt user to enable GPS, on OK switch to phone settings
goToSettings(error) {
  console.log("error: ", error);
  if(window.confirm("You need to enable location settings to use the geolocation feature.")) {
    cordova.plugins.diagnostic.switchToSettings();
  }
}

checkEnabled(); // Run check

希望这可以帮助其他提出这个答案的人。

于 2017-05-10T02:56:34.303 回答
0

使用 apache cordova 3 安卓平台

在 geoBroker.java 文件中的 execute 方法中,在 locationManager 实例化后添加以下操作。

if(action.equals("isGPSEnabled")){
            PluginResult result;
            if ( locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER )){
                result = new PluginResult(PluginResult.Status.OK);
            }else{
                result = new PluginResult(PluginResult.Status.ERROR);
            }
            callbackContext.sendPluginResult(result);
        }

然后在资产的 plugins 文件夹中的 geolocation.js 文件中添加公开新功能

 /**
     * Asynchronously checks if gps is enabled.
     *
     * @param {Function} successCallback    The function to call when gps is enabled.
     * @param {Function} errorCallback      The function to call when gps is not enabled. (OPTIONAL)
     */
    isGPSEnabled:function(successCallback, errorCallback){
        exec(successCallback, errorCallback, "Geolocation", "isGPSEnabled", []);
    }

希望能帮助到你

于 2013-10-21T15:34:50.257 回答
0

仅针对此处重定向的新用户,有一个名为Cordova 诊断插件的插件(大约在 2014 年年中开始) :

这个适用于 iOS、Android 和 Windows 10 Mobile 的 Cordova/Phonegap 插件用于管理设备设置,例如位置、蓝牙和 WiFi。它可以管理运行时权限、设备硬件和核心操作系统功能。ref

于 2016-07-02T05:39:15.997 回答