地理位置总是在 cordova 应用程序中超时
Geolocation always timing out in cordova app
我正在开发使用 Cordova 3.5.0-0.2.7 打包的 Sencha Touch (2.3.1) 应用程序。我正在尝试使用以下方法读取 GPS 坐标:
navigator.geolocation.getCurrentPosition()
但是在 Android 手机上请求总是超时,而在 Chrome 模拟器中它工作正常。我也尝试过使用 watchPosition()。
如有任何帮助,我们将不胜感激。
如果你还没有,安装 cordova-plugin-geolocation into your project - i.e. cordova plugin add cordova-plugin-geolocation
- this will add the appropriate permissions to your Android manifest, as Mike Dailor 正确地指出你需要。
您将哪些选项作为第三个参数传递给 getCurrentPosition()
? geolocationOptions 对象有 3 个属性:timeout、maxAge 和 enableHighAccuracy。
假设您想要一个准确的位置(即 GPS tracking/sat-nav 类型的应用程序),设置 enableHighAccuracy: true
会使您的应用程序要求 OS 使用 GPS 硬件检索位置。在这种情况下,您需要设置一个超时值,让 GPS 硬件有足够的时间第一次获得定位,否则在它有机会获得定位之前就会发生超时。
还要记住,在 Android 设备上关闭 GPS 的效果(例如,将定位模式设置更改为 "Battery Saving")因 Android 版本而异:要么OS 永远无法检索高精度位置,因此会发生超时错误(PERMISSION_DENIED 不会在 Android 上接收到)或者将检索并传递低精度位置使用 Wifi/cell 三角测量。
我建议使用 watchPosition() 而不是 getCurrentPosition() 来检索位置; getCurrentPosition() 在当前时间点对设备位置发出单一请求,因此位置超时可能发生在设备上的 GPS 硬件有机会获得定位之前,而使用 watchPosition() 您可以设置一个每次 OS 从 GPS 硬件接收到位置更新时都会调用成功函数的 watcher。如果您只想要一个位置,请在收到足够准确的位置后清除观察者。如果添加观察者时 Android 设备上的 GPS 关闭,它将继续 return 超时错误;我的解决方法是在出现一系列后续错误后清除并重新添加观察者。
所以按照这些思路:
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 (err.code == 3 // TIMEOUT
&& watchpositionErrorCount >= MAX_POSITION_ERRORS_BEFORE_RESET) {
clearWatch();
addWatch();
watchpositionErrorCount = 0;
}
}
addWatch();
我遇到了同样的问题,并通过这样做找到了解决方案:
添加插件白名单:ionic plugin add https://github.com/apache/cordova-plugin-whitelist.git
每次更改依赖项时不要忘记执行 npm 安装。
我想这个问题与权限有关,白名单解决了它。
祝你好运!
一直在与这个问题作斗争,直到我找到这个插件 => https://github.com/louisbl/cordova-plugin-locationservices。
该插件的存在主要是因为 cordova 地理定位插件不再使用 Android 代码:https://issues.apache.org/jira/browse/CB-5977。它依赖于 WebView 的地理定位功能。不知道为什么没人讨论这个。
这是我的代码片段:
$scope.getCordinates= function() {
if (window.cordova) {
var PRIORITY_BALANCED_POWER_ACCURACY = 102;
var posOptions = { maximumAge: 1000, timeout: 20000,enableHighAccuracy: false, priority: PRIORITY_BALANCED_POWER_ACCURACY };
cordova.plugins.locationServices.geolocation.getCurrentPosition(function(position) {
if(Location.init(position.coords.latitude,position.coords.longitude)){
$scope.Data.latitude = position.coords.latitude;
$scope.Data.longitude = position.coords.longitude;
//Toast.show('Getting Location...', 'short', 'top');
}else{
$scope.Data.latitude = '';
$scope.Data.longitude = '';
Toast.show('Turn on Location access in settings for better sales tracking..', 'short', 'bottom');
}
console.log(position);
}, function(error) {
$scope.Data.latitude = '';
$scope.Data.longitude = '';
Toast.show('Error encountered with Geolocation Api', 'short', 'bottom');
console.log(error);
}, posOptions);
}
}
我正在开发使用 Cordova 3.5.0-0.2.7 打包的 Sencha Touch (2.3.1) 应用程序。我正在尝试使用以下方法读取 GPS 坐标:
navigator.geolocation.getCurrentPosition()
但是在 Android 手机上请求总是超时,而在 Chrome 模拟器中它工作正常。我也尝试过使用 watchPosition()。
如有任何帮助,我们将不胜感激。
如果你还没有,安装 cordova-plugin-geolocation into your project - i.e. cordova plugin add cordova-plugin-geolocation
- this will add the appropriate permissions to your Android manifest, as Mike Dailor 正确地指出你需要。
您将哪些选项作为第三个参数传递给 getCurrentPosition()
? geolocationOptions 对象有 3 个属性:timeout、maxAge 和 enableHighAccuracy。
假设您想要一个准确的位置(即 GPS tracking/sat-nav 类型的应用程序),设置 enableHighAccuracy: true
会使您的应用程序要求 OS 使用 GPS 硬件检索位置。在这种情况下,您需要设置一个超时值,让 GPS 硬件有足够的时间第一次获得定位,否则在它有机会获得定位之前就会发生超时。
还要记住,在 Android 设备上关闭 GPS 的效果(例如,将定位模式设置更改为 "Battery Saving")因 Android 版本而异:要么OS 永远无法检索高精度位置,因此会发生超时错误(PERMISSION_DENIED 不会在 Android 上接收到)或者将检索并传递低精度位置使用 Wifi/cell 三角测量。
我建议使用 watchPosition() 而不是 getCurrentPosition() 来检索位置; getCurrentPosition() 在当前时间点对设备位置发出单一请求,因此位置超时可能发生在设备上的 GPS 硬件有机会获得定位之前,而使用 watchPosition() 您可以设置一个每次 OS 从 GPS 硬件接收到位置更新时都会调用成功函数的 watcher。如果您只想要一个位置,请在收到足够准确的位置后清除观察者。如果添加观察者时 Android 设备上的 GPS 关闭,它将继续 return 超时错误;我的解决方法是在出现一系列后续错误后清除并重新添加观察者。
所以按照这些思路:
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 (err.code == 3 // TIMEOUT
&& watchpositionErrorCount >= MAX_POSITION_ERRORS_BEFORE_RESET) {
clearWatch();
addWatch();
watchpositionErrorCount = 0;
}
}
addWatch();
我遇到了同样的问题,并通过这样做找到了解决方案:
添加插件白名单:ionic plugin add https://github.com/apache/cordova-plugin-whitelist.git
每次更改依赖项时不要忘记执行 npm 安装。
我想这个问题与权限有关,白名单解决了它。
祝你好运!
一直在与这个问题作斗争,直到我找到这个插件 => https://github.com/louisbl/cordova-plugin-locationservices。
该插件的存在主要是因为 cordova 地理定位插件不再使用 Android 代码:https://issues.apache.org/jira/browse/CB-5977。它依赖于 WebView 的地理定位功能。不知道为什么没人讨论这个。
这是我的代码片段:
$scope.getCordinates= function() {if (window.cordova) { var PRIORITY_BALANCED_POWER_ACCURACY = 102; var posOptions = { maximumAge: 1000, timeout: 20000,enableHighAccuracy: false, priority: PRIORITY_BALANCED_POWER_ACCURACY }; cordova.plugins.locationServices.geolocation.getCurrentPosition(function(position) { if(Location.init(position.coords.latitude,position.coords.longitude)){ $scope.Data.latitude = position.coords.latitude; $scope.Data.longitude = position.coords.longitude; //Toast.show('Getting Location...', 'short', 'top'); }else{ $scope.Data.latitude = ''; $scope.Data.longitude = ''; Toast.show('Turn on Location access in settings for better sales tracking..', 'short', 'bottom'); } console.log(position); }, function(error) { $scope.Data.latitude = ''; $scope.Data.longitude = ''; Toast.show('Error encountered with Geolocation Api', 'short', 'bottom'); console.log(error); }, posOptions); } }