我正在开发一个基于cordova的应用程序,它具有一些额外的功能,使用地理定位 .

因此,启动过程中的第一步是检查设备上是否启用了地理定位 . 检查是使用navigator.geolocation.getCurrentPosition并等待其中一个回调 .

但我刚刚发现,这个JS代码在所有设备上都没有按预期工作(在2个Androids上测试):

function checkPositionAvailability(){
    // tested both -> same result
    // var options = {maximumAge: 0, timeout: 10000, enableHighAccuracy:true};
    var options = {maximumAge: 0, timeout: 10000, enableHighAccuracy:false};

    navigator.geolocation.getCurrentPosition(
        function(position){
            console.log('got a position:');
            // ok, geolocation is available
        },
        function(error){
            console.log('got an error:');
            console.log('code   : ' + error.code);
            console.log('message: ' + error.message);

            console.log('error.PERMISSION_DENIED    = ' + error.PERMISSION_DENIED);
            console.log('error.POSITION_UNAVAILABLE = ' + error.POSITION_UNAVAILABLE);
            console.log('error.TIMEOUT              = ' + error.TIMEOUT);
            console.log('error.UNKNOWN_ERROR        = ' + error.UNKNOWN_ERROR);

            if (error.code == error.PERMISSION_DENIED) {
                // -> notify user that geolocation could be enabled
            }
        },
        options);
}

checkPositionAvailability();

禁用地理位置时的预期行为是:应使用POSITION_UNAVAILABLE或PERMISSION_DENIED错误代码立即调用错误回调 . 这是在使用Android 2.3.5的古老华为8850 . 但不是我使用Android 4.4.4的全新Moto G . 在Moto G上我必须等待整整10秒,然后才会出现TIMEOUT错误 .

这是一个错误吗?如果是,谁负责:Cordova,WebView或Android?有没有使用cordova的解决方法?或者我是否必须添加一些Java代码或插件?

另一个问题是:其他平台怎么样 - 上面的代码是否有效?

看来,有一种方法可以检测Android / Java的地理位置可用性 . 在Moto G上运行GPS状态应用程序时,我会立即收到有关已停用GPS的通知 .

这是我的两个设备的输出,运行我的cordova应用程序:

Huawei:
got an error:
code   : 2
message: The last location provider was disabled
error.PERMISSION_DENIED    = 1
error.POSITION_UNAVAILABLE = 2
error.TIMEOUT              = 3
error.UNKNOWN_ERROR        = undefined


Moto G:
got an error:
code   : 3
message: Timeout expired
error.PERMISSION_DENIED    = 1
error.POSITION_UNAVAILABLE = 2
error.TIMEOUT              = 3
error.UNKNOWN_ERROR        = undefined

Cordova版本是3.5.0-0.2.7