phonegap:如何检查是否启用了 gps
phonegap: how to check if gps is enabled
我想显示一个警告说 "Please turn on your 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>
您问题的答案在下方。
您无法检查*是否启用了 gps*
可以设置超时,获取超时。
您可以通过在 geolocationoptions 参数中设置 timeout
参数来实现。 (您没有使用)
- 超时: 从调用
navigator.geolocation.getCurrentPosition
或 geolocation.watchPosition
到对应的 geolocationSuccess
回调执行。如果在此时间内未调用 geolocationSuccess
回调,则会向 geolocationError
回调传递一个 PositionError.TIMEOUT
错误代码。 (请注意,当与 geolocation.watchPosition
结合使用时,geolocationError
回调可以每隔 timeout
毫秒调用一次!)(Number)
等等,我错了
现在有一个插件可以检查 GPS 是否打开。
其实不止一个
检查 GPS、Wifi 等是否已启用的最佳方法是使用此插件:
https://www.npmjs.com/package/cordova.plugins.diagnostic
使用这个插件你可以检查很多功能,你可以切换用户到设置。
假设我们只讨论 Android 平台,因为 @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);
});
如果结果是 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);
我想显示一个警告说 "Please turn on your 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>
您问题的答案在下方。
您无法检查*是否启用了 gps*
可以设置超时,获取超时。
您可以通过在 geolocationoptions 参数中设置 timeout
参数来实现。 (您没有使用)
- 超时: 从调用
navigator.geolocation.getCurrentPosition
或geolocation.watchPosition
到对应的geolocationSuccess
回调执行。如果在此时间内未调用geolocationSuccess
回调,则会向geolocationError
回调传递一个PositionError.TIMEOUT
错误代码。 (请注意,当与geolocation.watchPosition
结合使用时,geolocationError
回调可以每隔timeout
毫秒调用一次!)(Number)
等等,我错了
现在有一个插件可以检查 GPS 是否打开。
其实不止一个
检查 GPS、Wifi 等是否已启用的最佳方法是使用此插件:
https://www.npmjs.com/package/cordova.plugins.diagnostic
使用这个插件你可以检查很多功能,你可以切换用户到设置。
假设我们只讨论 Android 平台,因为 @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);
});
如果结果是 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);