android geolocation使用phonegap代码:3错误 [英] android geolocation using phonegap code: 3 error
问题描述
我尝试使用PhoneGap API文件使用地理位置,但收到以下错误讯息:
提醒
code:3
message:Timeout已过期
我的代码为
< script&
//等待Cordova加载
//
document.addEventListener(deviceready,onDeviceReady,false);
var watchID = null;
// Cordova准备就绪
//
函数onDeviceReady(){
//如果每30秒没有收到更新就抛出一个错误
var options = {timeout:10000};
watchID = navigator.geolocation.watchPosition(onSuccess,onError,options);
}
// onSuccess地理位置
//
function onSuccess(position){
var element = document.getElementById('geolocation');
element.innerHTML ='Latitude:'+ position.coords.latitude +'< br />'+
'经度:'+ position.coords.longitude +'< br / '+
'< hr />'+ element.innerHTML;
}
// onError Callback接收一个PositionError对象
//
函数onError(error){
alert('code:'+ error。 code +'\\\
'+
'message:'+ error.message +'\\\
');
}
假设你在Android模拟器中遇到这个问题:
-
添加超时并设置enableHighAccuracy:
navigator .geolocation.getCurrentPosition(onSuccess,onError,{timeout:10000,enableHighAccuracy:true});
在某些模拟器中,您需要将enableHighAccuracy设置为false,所以如果仍然不能工作,请尝试。
-
在Android中,模拟器不读取GPS值,因此我们需要通过命令行发送。我们需要在模拟器运行的端口启动一个telnet会话(你可以在模拟器窗口标题,在开头的数字,在我的情况下5554)检查端口:
telnet localhost 5554
然后运行命令
geo fix -122.4 37.78
如果您关闭应用程序,则需要重新发送地理位置,因此如果无法运行,只需运行 如果您使用的是真实的Android设备,请确保该设备已启动已启用GPS,并且已安装geolocation插件(运行 I'm trying to use geolocation using PhoneGap API doc but getting below error message, Alert
code:3
message: Timeout expired My code is, Please advice me what I am missing. Assuming you are getting this problem in an Android emulator: Add a timeout and set enableHighAccuracy: In certain emulators you need to set enableHighAccuracy to false, so try that if still doesn't work. In Android, the emulator don’t read GPS values, so we need to send them via command line. We need to start a telnet session in the port that the emulator is running (you can check the port in the emulator window title, the number at the beginning, in my case 5554): And then run the command If you close the app you need to re-send the geolocation, so if it doesn’t work, just run the If you are using a real Android device, make sure it has GPS enabled, and that you installed the geolocation plugin (run 这篇关于android geolocation使用phonegap代码:3错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
phonegap local plugin add org.apache.cordova.geolocation
) <script>
// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);
var watchID = null;
// Cordova is ready
//
function onDeviceReady() {
// Throw an error if no update is received every 30 seconds
var options = { timeout: 10000 };
watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
}
// onSuccess Geolocation
//
function onSuccess(position) {
var element = document.getElementById('geolocation');
element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +
'Longitude: ' + position.coords.longitude + '<br />' +
'<hr />' + element.innerHTML;
}
// onError Callback receives a PositionError object
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
navigator.geolocation.getCurrentPosition(onSuccess, onError, {timeout: 10000, enableHighAccuracy: true});
telnet localhost 5554
geo fix -122.4 37.78
geo fix
command just after opening the app, before the timeout event fires.phonegap local plugin add org.apache.cordova.geolocation
)