android – phonegap:如何检查是否启用了gps

前端之家收集整理的这篇文章主要介绍了android – phonegap:如何检查是否启用了gps前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想显示一条警告说“请打开你的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);
原文链接:https://www.f2er.com/android/314238.html

猜你在找的Android相关文章