【问题标题】:Cordova/Phonegap geolocation plugin manage errorCordova/Phonegap 地理定位插件管理错误
【发布时间】:2015-07-23 10:08:11
【问题描述】:

我正在编写一个 Cordova/Phonegap 应用程序,我使用了一个地理定位插件...这是我的代码...

var onSuccess = function(position) {
    longitude = position.coords.longitude;
    latitude = position.coords.latitude;

    console.log("Latitude: "+position.coords.latitude);
    console.log("Longitude: "+position.coords.longitude);
};

function onError(error) {
    logService.debug("Code: "+error.code);
    logService.debug("Message: "+error.message);
};

navigator.geolocation.getCurrentPosition(onSuccess, onError, { maximumAge: 3000, timeout: 15000, enableHighAccuracy: true });

现在,我在浏览器上对其进行测试,当我没有获得权限时,我收到错误代码 1 (PositionError.PERMISSION_DENIED),当我获得权限时,它在浏览器上运行良好......现在出生的问题......当我在设备上测试这个并且 GPS 关闭时,我没有收到错误代码 1 (PositionError.PERMISSION_DENIED) 但总是收到超时...这样我就看不出区别...我将超时设置为150000 但我总是收到代码 3 (PositionError.TIMEOUT)...为什么?怎样才能正确使用呢?

【问题讨论】:

  • 当设备的 gps 关闭时,您实际上想要什么?让用户打开它?
  • 我想在我的控制台中读取...代码 1 (PositionError.PERMISSION_DENIED) 或代码 2 (PositionError.POSITION_UNAVAILABLE)...它不能总是超时...
  • 即使关闭GPS,也应该可以通过3G网络、wifi等获取位置。反正geolocation插件使用的是geolocation内置的native webview,所以怪google(by责怪我真的是说,在android问题跟踪器上打开一个问题)

标签: javascript android cordova geolocation phonegap-plugins


【解决方案1】:

我发现地理定位错误的处理是特定于平台的。由于设置 enableHighAccuracy: true 会导致您的应用要求操作系统使用 GPS 硬件检索位置,因此在 Android 设备上关闭 GPS 的效果因 Android 版本而异:操作系统永远无法检索高精度位置,因此会发生 TIMEOUT 错误(在 Android 上不会收到 PERMISSION_DENIED),或者将检索并传递低精度位置,而不是使用 Wifi/cell 三角测量。

我建议使用watchPosition() 而不是getCurrentPosition() 来检索位置; getCurrentPosition() 在当前时间点对设备位置发出单个请求,因此在设备上的 GPS 硬件有机会获得定位之前可能会发生位置超时,而使用 watchPosition() 您可以设置每次操作系统从 GPS 硬件接收到位置更新时都会调用成功函数的 watcher。如果您只想要一个位置,请在收到足够准确的位置后清除观察者。如果在添加watcher时Android设备上关闭了GPS,会继续返回TIMEOUT错误;我的解决方法是在出现一系列错误后清除并重新添加观察者。

所以尝试以下方法:

var MAX_POSITION_ERRORS_BEFORE_RESET = 3,
MIN_ACCURACY_IN_METRES = 20,
positionWatchId = null, 
watchpositionErrorCount = 0,
options = {
    maximumAge: 60000, 
    timeout: 15000, 
    enableHighAccuracy: true
};

function addWatch(){
    positionWatchId = navigator.geolocation.watchPosition(onWatchPositionSuccess, onWatchPositionError, options);
}

function clearWatch(){
    navigator.geolocation.clearWatch(positionWatchId);
}

function onWatchPositionSuccess(position) {
    watchpositionErrorCount = 0;

    // Reject if accuracy is not sufficient
    if(position.coords.accuracy > MIN_ACCURACY_IN_METRES){
      return;        
    }

    // If only single position is required, clear watcher
    clearWatch();

    // Do something with position
    var lat = position.coords.latitude,   
    lon = position.coords.longitude;
}


function onWatchPositionError(err) {
    watchpositionErrorCount++;
    if (watchpositionErrorCount >= MAX_POSITION_ERRORS_BEFORE_RESET) {        
        clearWatch();
        addWatch();
        watchpositionErrorCount = 0;
    }

}
addWatch();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-12
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 2017-01-24
    • 1970-01-01
    相关资源
    最近更新 更多