我拥有一个 Ionic 应用程序 (Cordova)。
我有这个 JS 代码,旨在获取 Android 设备的位置:
$cordovaGeolocation.getCurrentPosition({
enableHighAccuracy: true,
timeout: 15000
})
$cordovaGeolocation
来自 ng-cordova库。
我成功检查了插件 org.apache.cordova.geolocation
是否已更新为最新版本。
一些用户提示第一次查询需要 15 秒!
请注意,超时设置为 15000 毫秒 => 15 秒。
这意味着设备未能成功确定位置。
经过调查,我发现了问题:
这些用户已在其设备设置中将其位置模式设置为“仅限设备”或也称为“仅限 GPS”。
当用户切换到“高精度模式”时,整个过程只需不到一秒。
如何在不建议用户切换到“高精度模式”的情况下解决此问题? 这是一个错误吗?
请注意,我还使用 enableHighAccuracy: false
进行了测试,但结果相同。
我不是唯一一个遇到这个“大”问题的人:
Phonegap - Geolocation with PowerSaving and GPS Only Mode
但没有得到答复..
最佳答案
我见过你issue on github 。 我正在使用 ng-cordova 插件。我使用以下解决方法来使仅 GPS 工作。由于低精度速度较快,超时时间为4s。
$cordovaGeolocation.getCurrentPosition({enableHighAccuracy: false, maximumAge: MAXAGE, timeout: 4000})
.then(
function (position) { //success Low-Accuracy
console.log('getCurrentPosition: HighAccuracy false: Ok!');
//[..]
},
function(err) { //error Low-Accuracy
console.log(err);
$cordovaGeolocation.getCurrentPosition({enableHighAccuracy: true, maximumAge: MAXAGE, timeout: 10000})
.then(
function (position) { //success High-Accuracy
console.log('getCurrentPosition: HighAccuracy true: Ok!');
//[..]
},
function(err) { //error High-Accuracy
console.log('getLocation: ERRO: ' + ERROR[err.code] + ' => ' + err.message);
//[..]
}
);
}
);
关于javascript - 当位置模式设置为 "Device only"时,总是达到有关地理位置的超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29411697/