gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:07:49 25 4
gpt4 key购买 nike

我想显示一条提示“请打开您的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com