android - 电话间隙 : how to check if gps is enabled

标签 android cordova gps phonegap-plugins phonegap-build

我想显示一条提示“请打开您的 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 平台,如 @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);

关于android - 电话间隙 : how to check if gps is enabled,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35304103/

相关文章:

ios - 是否可以将托管网络应用程序放入 Apple App Store (2016)?

android - 布局中资源的使用

android - 操作栏样式 [顶部和底部]

android - 使用带有phonegap的Google登录来获取用户信息

javascript - Phonegap - 如何从 base64 字符串生成图像文件?

c# - Windows Mobile 6 GPS 精度

java - 发送到数据库时 Android GPS 延迟

java - 最新版本的 Android Studio 中 TextView 的文本对齐选项在哪里?

java - 在匿名对象上调用父类(super class)方法 AsyncTask.execute() new HttpRequestTask<...> extends AsyncTask<...>

java - Android LocationListener 返回空值