【问题标题】:phonegap: how to check if gps is enabledphonegap:如何检查 gps 是否已启用
【发布时间】:2016-02-09 23:16:28
【问题描述】:

我想显示“请打开 GPS”的提醒。

如何使用 phonegap 获取 GPS 状态?

我使用了以下代码,但如果 GPS 关闭,我不会收到任何警报消息。相反,什么也没有发生。

<!DOCTYPE html>
  <html>
  <head>
   <title>Device Properties Example</title>

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">

// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);

// device APIs are available
//
function onDeviceReady() {

   var test= navigator.geolocation.getCurrentPosition(onSuccess, onError);

}

// onSuccess Geolocation
//
function onSuccess(position) {
    var element = document.getElementById('geolocation');
    element.innerHTML = 'Latitude: '           + position.coords.latitude              + '<br />' +
                        'Longitude: '          + position.coords.longitude             + '<br />' +
                        'Altitude: '           + position.coords.altitude              + '<br />' +
                        'Accuracy: '           + position.coords.accuracy              + '<br />' +
                        'Altitude Accuracy: '  + position.coords.altitudeAccuracy      + '<br />' +
                        'Heading: '            + position.coords.heading               + '<br />' +
                        'Speed: '              + position.coords.speed                 + '<br />' +
                        'Timestamp: '          + position.timestamp                    + '<br />';
}

// onError Callback receives a PositionError object
//
function onError(error) {
    alert('code: '    + error.code    + '\n' +
          'message: ' + error.message + '\n');
}

</script>
</head>
<body>
  <p id="geolocation">   Finding geolocation...</p>
</body>
</html>

【问题讨论】:

    标签: android cordova gps phonegap-plugins phonegap-build


    【解决方案1】:

    假设我们只讨论 Android 平台,正如@Joerg 正确所说,您可以使用cordova.plugins.diagnostic 来检查是否使用isGpsLocationEnabled() 启用了 GPS:

    cordova.plugins.diagnostic.isGpsLocationEnabled(function(enabled){
        console.log("GPS location is " + (enabled ? "enabled" : "disabled"));
    }, function(error){
        console.error("The following error occurred: "+error);
    });    
    

    如果结果是 GPS 关闭,您可以将用户切换到位置设置页面以手动启用 GPS:

    cordova.plugins.diagnostic.switchToLocationSettings();
    

    或者,另外,您可以使用cordova-plugin-request-location-accuracy 直接从应用程序内请求高精度定位模式(即 GPS)。这将显示一个原生确认对话框,如果用户同意,GPS 将自动启用:

    function onRequestSuccess(success){
        console.log("Successfully requested accuracy: "+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(window.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(onRequestSuccess, onRequestFailure, cordova.plugins.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY);
    

    【讨论】:

    • 感谢您的出色回答。但不幸的是,这个插件在 Phonegap Build 中不起作用。每当我在 config.xml 中包含插件时,它都会因 android 错误而失败。
    • 我不使用 Phonegap Build(我在本地构建),但我很想知道错误是什么以及是否可以修复它。如果您能在 plugin's github repo 上打开一个问题并粘贴来自 Phonegap Build 的包含错误详细信息的日志输出,我将不胜感激。
    • iOS 不区分高精度 (GPS) 或低精度(网络) - 它只是“位置”。您可以使用诊断插件检查isLocationEnabled(),如果没有,我们可以做的最好的就是switchToSettings 允许用户手动启用它。
    • 请注意,可以通过请求位置在 iOS 上本地调用此类对话框,但这是 cordova-plugin-geolocation 目前不支持的 - 请参阅 this issue
    【解决方案2】:

    您的问题的答案在下面

    您无法检查*是否启用了 gps*

    您可以设置超时并获取超时。 您可以通过在geolocationoptions 参数中设置timeout 参数来做到这一点。 (你没有使用)

    来自documentation

    • 超时: 允许从调用navigator.geolocation.getCurrentPositiongeolocation.watchPosition 到执行相应的geolocationSuccess 回调的最大时间长度(毫秒)。如果在此时间内未调用 geolocationSuccess 回调,则向 geolocationError 回调传递 PositionError.TIMEOUT 错误代码。 (请注意,当与geolocation.watchPosition 结合使用时,geolocationError 回调可以每隔timeout 毫秒调用一次!)(数字)

    等等我错了

    现在有一个插件可以检查 GPS 是否打开。

    其实不止一个。

    【讨论】:

    • 感谢您的回复。我们可以使用phonegap触发在android中打开gps吗?
    • @user2899728 如果答案解决了您的问题,您能否勾选答案“已接受”。
    • 您能否建议我如何触发 gps 设置弹出窗口,以便用户可以在应用内打开它?
    • 你测试过这两个插件了吗?他们没有为我工作。所以我试图找到使用诊断插件的正确方法。
    【解决方案3】:

    检查 GPS、Wifi 等是否已启用的最佳方法是使用此插件:

    https://www.npmjs.com/package/cordova.plugins.diagnostic

    使用此插件,您可以检查很多功能,并且可以将用户切换到设置。

    【讨论】:

    • 我一直在使用诊断插件中的 isLocationEnabled(),但如果我的 Gps 关闭,该函数将转到成功回调。我必须使用 isGpsLocationEnabled() 和 isNetworkLocationEnabled() 来代替吗?有什么区别?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-27
    • 2016-07-07
    • 1970-01-01
    相关资源
    最近更新 更多