gpt4 book ai didi

android - phonegap 地理位置总是在超时时失败

转载 作者:IT老高 更新时间:2023-10-28 23:28:29 26 4
gpt4 key购买 nike

我在我的应用程序中使用 sencha-touch 2.0 和 phonegap 2.0.0 来检索用户的位置。在我的本地主机上运行时,一切正常。但是,当将 .apk 加载到我的 android 15 api 设备时(使用 eclipse 和 adt 插件),每次调用 getCurrentLocation 或 watchPosition 都不会返回...

这是我的代码:

geoOn: function(){
var geoReady = navigator.geolocation || undefined;

var onSuccess = function(position){
Top5.app.alert('Geolocation success '+String(position.coords.latitude) + ' ' + String(position.coords.longitude),'Geolocation');
var scope = Ext.getCmp('nestedList');
scope.updateDistance(position.coords);
};

var onFailure = function(error){Top5.app.alert('Geolocation failed: '+String(error.code) + ' '+String(error.message),'Geolocation');};
if (geoReady) {
this.watchId = navigator.geolocation.watchPosition(onSuccess ,onFailure,{timeout:6000,maximumAge: 3000,enableHighAccuracy: true});
}
else{
Ext.device.Geolocation.watchPosition({
frequency: 3000, // Update every 3 seconds
callback: function(position) {
this.updateDistance(position.coords);
},
failure: function() {
console.log('Geolocation Failure!');
},
scope:this
});
}
},
geoGet: function(){
var geoReady = navigator.geolocation || undefined;
if (geoReady) {

var onSuccess = function(position){
Top5.app.alert('Geolocation successful!!!');
var scope = Ext.getCmp('nestedList');
scope.updateDistance(position.coords);
};
var onFailure = function(error){Top5.app.alert('Geolocation failed: '+String(error.code) + ' '+String(error.message),'Geolocation');};
navigator.geolocation.getCurrentPosition(onSuccess,onFailure);
}
else{}
},
geoOff:function(){

var geoReady = navigator.geolocation || undefined;
if (geoReady) {
navigator.geolocation.clearWatch(this.watchId);
this.watchId = null;
}
else{
Ext.device.Geolocation.clearWatch();
}

},
updateDistance:function(coords){
Top5.app.alert('updateDist','');
var scope = Ext.getCmp('nestedList');
var lat = coords.latitude,lon = coords.longitude;
var store = scope.getStore();
var i,record;
for(i = 0; i < store.data.all.length; i++)
{
record = store.data.all[i];
if(record.data.locationX){
record.set('distance',Top5.app.getDistance(record.data.locationX,record.data.locationY,lat,lon).toFixed(3));
}
}
}

更新:所以我走出了我的大楼,它奏效了……我需要更频繁地外出。
但是,当我禁用 gps 时,我认为 geoLocation 会使用 wifi 连接找到我的位置 - 但它失败了(我设置 enableHighAccuracy: false)。这是为什么呢?

更新:让我重新表述我的问题:navigator.geolocation.watchPosition 是否可以同时使用 GPS 信号和 wifi/g3 信号?如何仅使用互联网连接检测用户位置?目前,我的代码仅适用于 GPS,当禁用该选项或信号被阻止时,地理定位不起作用。

最佳答案

我知道也许为时已晚,但今天我在同样的问题上苦苦挣扎!解决方案原来非常简单!

解决方案:重启设备。

就是这样。

问题是,你永远不知道我的用户什么时候会遇到这种错误,所以如果你的应用程序严重依赖地理定位,我建议你在位置选项中设置超时 navigator.geolocation.getCurrentPosition(geoSuccess, geoError , {timeout: 15000}) 并提醒用户有关问题和可能的解决方案。

附:请记住,地理定位可能会超时,例如,如果移动数据也被关闭,因此重新启动并不总是能解决它。在 iPhone 上,它使用蜂窝数据来获取您的位置,但在 Android 上,除非打开 3G,否则手机似乎无法访问蜂窝数据

关于android - phonegap 地理位置总是在超时时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12859425/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com