phonegap:如何检查是否启用gps [英] phonegap: how to check if gps is enabled

查看:148
本文介绍了phonegap:如何检查是否启用gps的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我如何使用phonegap获取GPS状态?



我使用了以下代码,但如果GPS关闭,我不会收到任何提醒消息。

 <!DOCTYPE html> 
< html>
< head>
< title>设备属性示例< / title>

< script type =text / javascriptcharset =utf-8src =cordova.js>< / script>
< script type =text / javascriptcharset =utf-8>

//等待设备API库加载
//
document.addEventListener(deviceready,onDeviceReady,false);

//设备API可用
//
函数onDeviceReady(){

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



// onSuccess地理位置
//
函数onSuccess(位置){
var element = document.getElementById('geolocation );
element.innerHTML ='Latitude:'+ position.coords.latitude +'< br />'+
'经度:'+ position.coords.longitude +'< br /> '+
'高度:'+ position.coords.altitude +'< br />'+
'准确度:'+ position.coords.accuracy +'< br />'+
'高度准确度:'+ position.coords.altitudeAccuracy +'< br />'+
'标题:'+ position.coords.heading +'< br />'+
'速度:'+ position.coords.speed +'< br />'+
'时间戳:'+ position.timestamp +'< br />';
}

// onError回调接收到一个PositionError对象
//
function onError(error){
alert('code:'+ error。 code +'\\\
'+
'message:'+ error.message +'\\\
');
}

< / script>
< / head>
< body>
< p id =geolocation>寻找地理位置...< / p>
< / body>
< / html>


解决方案

假设我们只谈论Android平台,正如 @Joerg 所说的那样,您可以使用 cordova.plugins.diagnostic 来检查GPS是否使用 isGpsLocationEnabled()

  cordova.plugins.diagnostic.isGpsLocationEnabled(function(enabled){
console.log(GPS location is+(enabled?enabled:disabled));
},function(错误){
console.error(发生以下错误:+错误);
});

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

  cordova.plugins.diagnostic.switchToLocationSettings(); 

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

 函数onRequestSuccess(成功){
console.log(成功请求准确性:+ success.message);

$ b $ 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(无法自动将位置模式设置为'高精度'。是否要切换到位置设置页面并手动执行此操作?)){
cordova.plugins.diagnostic.switchToLocationSettings();
}
}
}

cordova.plugins.locationAccuracy.request(onRequestSuccess,onRequestFailure,cordova.plugins.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY);


I want to show an alert saying that "Please turn on your GPS".

How can I get GPS status using phonegap?

I have used following code but I don't get any alert message if GPS is turned off. Instead nothing happens.

<!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>

解决方案

Assuming we're talking just about Android platform only, as @Joerg rightly says, you can use cordova.plugins.diagnostic to check if GPS is enabled using isGpsLocationEnabled():

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

If the result is that GPS is switched off, you can either switch the user to the location settings page to manually enable GPS:

cordova.plugins.diagnostic.switchToLocationSettings();

Or, in addition, you could use cordova-plugin-request-location-accuracy to request high accuracy location mode (i.e. GPS) directly from within the app. This will show a native confirm dialog and if user agrees, GPS will be enabled automatically:

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:如何检查是否启用gps的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆