简体   繁体   English

Phonegap Android Geolocation watchPosition超时

[英]Phonegap Android Geolocation watchPosition timeout

I am using Cordova 3.0.0, A Samsung Galaxy S3 4.1.2 to test. 我正在使用Cordova 3.0.0,三星Galaxy S3 4.1.2进行测试。

When I test the Phonegap full example for watching position : http://docs.phonegap.com/en/edge/cordova_geolocation_geolocation.md.html#geolocation.watchPosition 当我测试Phonegap完整示例以观察位置时: http ://docs.phonegap.com/en/edge/cordova_geolocation_geolocation.md.html#geolocation.watchPosition

I get one first line with coords, and then I got the error : 我得到一个带coords的第一行,然后我得到了错误:

code 3
message : Position retrieval timed out.

And no other location are returned. 并且不会返回其他位置。 The same code on my iphone is working. 我的iPhone上的代码相同。 I am testing when walking, and when I was in a car, same thing. 我在走路时测试,当我在车里时也是如此。

I noticed that when I use google maps on the android device, a gps icons pops up on the top bar. 我注意到当我在Android设备上使用谷歌地图时,顶部栏上会弹出一个gps图标。 When I launch my app, it is not here. 当我启动我的应用程序时,它不在这里。

The Manifest is correctly set up with permissions, and my options are : Manifest已正确设置权限,我的选项是:

var options = { maximumAge: 3000, timeout: 5000, enableHighAccuracy: true };
watchID = navigator.geolocation.watchPosition( onSuccess, onError, options );

I need precision for my app to work... 我的应用程序需要精确工作......

Thanks for help. 感谢帮助。

Ok, so after hours of tests, I managed to make the watchPosition work on Android with Cordova 3.1.0 . 好的,经过几个小时的测试后,我设法使用Cordova 3.1.0在Android上运行watchPosition。

What you have to do: Use the html5 only api for geolocation and do not include this : http://cordova.apache.org/docs/en/3.1.0/cordova_geolocation_geolocation.md.html#Geolocation 你需要做的:使用html5 only api进行地理定位,不要包括: http//cordova.apache.org/docs/en/3.1.0/cordova_geolocation_geolocation.md.html#Geolocation

If you already have it in your projet, remove it via the CLI : cordova plugin rm org.apache.cordova.geolocation 如果您已经在projet中使用它,请通过CLI将其删除:cordova plugin rm org.apache.cordova.geolocation

Of course, keep those lines in your android manifest : 当然,在你的android清单中保留这些行:

< uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
< uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
< uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />

This is working for me, so tell me if its working for you. 这对我有用,请告诉我它是否适合你。 I will post code if you need ( but the cordova full example works for example ) 如果需要,我会发布代码(但是完整的示例工作例如)

Maybe this can help you : 也许这可以帮助你:

I just discover one massive problem in Cordova/PhoneGap : - When you open the Android project built by cordova (in commandline), the requestUpdate is set at ..... 60 Seconds !!!!!!!!!!! 我刚刚发现了Cordova / PhoneGap中的一个大问题: - 当你打开由cordova构建的Android项目时(在命令行中),requestUpdate设置为..... 60秒!!!!!!!!!!! You have a new Location every Minute ! 每分钟都有一个新的位置! Evenif you use GPS (enableHighAccuracy="true"). 即使你使用GPS(enableHighAccuracy =“true”)。 So try to set this at 1000 where you find LocationManager.GPS_PROVIDER in org.apache.cordova.geolocation package -> GPSListener class ! 所以尝试将其设置为1000,在org.apache.cordova.geolocation包中找到LocationManager.GPS_PROVIDER - > GPSListener类!

Yes, you're right, there appears to be a problem with geolocation in Phonegap v3.x - I have 3.1.0-0.15.0 installed. 是的,你说得对,Phonegap v3.x中的地理位置似乎有问题 - 我安装了3.1.0-0.15.0。 I'm seeing the same: a single correct position, then a timeout error, triggering the clear/re-add of the watcher, followed by a correct position. 我看到的相同:一个正确的位置,然后是超时错误,触发观察者的清除/重新添加,然后是正确的位置。 Running the same test app on Phonegap 2.9.0, it works fine, successfully retrieving positions. 在Phonegap 2.9.0上运行相同的测试应用程序,它工作正常,成功检索位置。 This time I used the options { maximumAge: 0, timeout: 30000, enableHighAccuracy: true }; 这次我使用了选项{ maximumAge: 0, timeout: 30000, enableHighAccuracy: true };

See if you get the same results: here's the v3.1.0 project&APK and here's the v2.9.0 project&APK . 看看你是否得到了相同的结果: 这里是v3.1.0项目和APK这里是v2.9.0项目和APK

Here's the logcat when I built it with Phonegap 3.1.0: 这是我使用Phonegap 3.1.0构建它时的logcat:

10-24 20:50:29.287: I/Web Console(17092): Device Ready at :1171167449
10-24 20:50:59.287: I/Web Console(17092): Error while retrieving current position. Error code: 3,Message: Position retrieval timed out. at :1171167509
10-24 20:51:29.328: I/Web Console(17092): Error while retrieving current position. Error code: 3,Message: Position retrieval timed out. at :1171167509
10-24 20:51:59.337: I/Web Console(17092): Error while retrieving current position. Error code: 3,Message: Position retrieval timed out. at :1171167445
10-24 20:52:28.397: I/Web Console(17092): Position updated: latitude=50.64626501666667, longitude=-4.736916616666666, accuracy=17m at :-1468095491
10-24 20:52:28.397: I/Web Console(17092): Position updated: latitude=50.64626501666667, longitude=-4.736916616666666, accuracy=17m at :-1468095491
10-24 20:52:58.407: I/Web Console(17092): Error while retrieving current position. Error code: 3,Message: Position retrieval timed out. at :1171167445
10-24 20:53:27.878: I/Web Console(17092): Position updated: latitude=50.646433849999994, longitude=-4.736890766666666, accuracy=8m at :-1468095491
10-24 20:53:27.878: I/Web Console(17092): Position updated: latitude=50.646433849999994, longitude=-4.736890766666666, accuracy=8m at :-1468095491
10-24 20:53:57.887: I/Web Console(17092): Error while retrieving current position. Error code: 3,Message: Position retrieval timed out. at :1171167445
10-24 20:54:27.367: I/Web Console(17092): Position updated: latitude=50.64643435, longitude=-4.736896850000001, accuracy=9m at :-1468095491
10-24 20:54:27.367: I/Web Console(17092): Position updated: latitude=50.64643435, longitude=-4.736896850000001, accuracy=9m at :-1468095491
10-24 20:54:57.377: I/Web Console(17092): Error while retrieving current position. Error code: 3,Message: Position retrieval timed out. at :1171167445

And here's the logcat from exactly the same app when I built it using Phonegap v2.9.0: 当我使用Phonegap v2.9.0构建它时,这是来自完全相同的应用程序的logcat:

10-24 21:10:19.698: I/Web Console(19755): Device Ready at :1172318425
10-24 21:10:22.487: I/Web Console(19755): Position update rejected because accuracy of45m is less than required 20m at :1172318349
10-24 21:10:54.697: I/Web Console(19755): Position updated: latitude=50.646209633333335, longitude=-4.737049566666667, accuracy=9m at :1172318421
10-24 21:10:55.177: I/Web Console(19755): Position updated: latitude=50.64623646666667, longitude=-4.7369963, accuracy=9m at :1172318421
10-24 21:10:56.177: I/Web Console(19755): Position updated: latitude=50.64623674999999, longitude=-4.736949983333333, accuracy=9m at :1172318357
10-24 21:10:57.677: I/Web Console(19755): Position updated: latitude=50.646237033333335, longitude=-4.73695015, accuracy=9m at :1172318357
10-24 21:10:58.677: I/Web Console(19755): Position updated: latitude=50.646238333333336, longitude=-4.736944716666667, accuracy=9m at :1172318357
10-24 21:10:59.747: I/Web Console(19755): Position updated: latitude=50.6462348, longitude=-4.736952216666666, accuracy=9m at :1172318357
10-24 21:11:00.667: I/Web Console(19755): Position updated: latitude=50.64623206666667, longitude=-4.73695695, accuracy=9m at :1172318357
10-24 21:11:01.677: I/Web Console(19755): Position updated: latitude=50.646232700000006, longitude=-4.7369544999999995, accuracy=9m at :1172318357
10-24 21:11:02.677: I/Web Console(19755): Position updated: latitude=50.646232700000006, longitude=-4.7369544999999995, accuracy=12m at :1172318357
10-24 21:11:03.687: I/Web Console(19755): Position updated: latitude=50.64623241666666, longitude=-4.736952116666667, accuracy=12m at :1172318357
10-24 21:11:04.207: I/Web Console(19755): Position updated: latitude=50.646240533333334, longitude=-4.736951266666667, accuracy=12m at :1172318357
10-24 21:11:05.187: I/Web Console(19755): Position updated: latitude=50.646235399999995, longitude=-4.736953183333334, accuracy=12m at :1172318357
10-24 21:11:06.197: I/Web Console(19755): Position updated: latitude=50.6462522, longitude=-4.736926933333334, accuracy=12m at :1172318357
10-24 21:11:07.197: I/Web Console(19755): Position updated: latitude=50.646253949999995, longitude=-4.736931416666667, accuracy=12m at :1172318357
10-24 21:11:07.517: I/Web Console(19755): Position update rejected because accuracy of45m is less than required 20m at :1172318357
10-24 21:11:08.197: I/Web Console(19755): Position updated: latitude=50.64624196666667, longitude=-4.736951366666666, accuracy=9m at :1172318357
10-24 21:11:09.697: I/Web Console(19755): Position updated: latitude=50.646261833333334, longitude=-4.73694825, accuracy=9m at :1172318357
10-24 21:11:10.697: I/Web Console(19755): Position updated: latitude=50.64627816666666, longitude=-4.736968816666666, accuracy=9m at :1172318357
10-24 21:11:11.197: I/Web Console(19755): Position updated: latitude=50.64627698333334, longitude=-4.736982066666667, accuracy=9m at :1172318357
10-24 21:11:12.197: I/Web Console(19755): Position updated: latitude=50.646291033333334, longitude=-4.736992866666667, accuracy=9m at :1172318357
10-24 21:11:13.197: I/Web Console(19755): Position updated: latitude=50.646285549999995, longitude=-4.73696835, accuracy=9m at :1172318357
10-24 21:11:14.197: I/Web Console(19755): Position updated: latitude=50.64628801666666, longitude=-4.736988766666667, accuracy=9m at :1172318357
10-24 21:11:15.197: I/Web Console(19755): Position updated: latitude=50.646256433333335, longitude=-4.736974683333333, accuracy=9m at :1172318357
10-24 21:11:16.197: I/Web Console(19755): Position updated: latitude=50.64623895, longitude=-4.736980716666666, accuracy=9m at :1172318357
10-24 21:11:17.207: I/Web Console(19755): Position updated: latitude=50.646249616666665, longitude=-4.736948533333333, accuracy=9m at :1172318357
10-24 21:11:18.207: I/Web Console(19755): Position updated: latitude=50.64624111666667, longitude=-4.7369412833333335, accuracy=9m at :1172318357
10-24 21:11:19.217: I/Web Console(19755): Position updated: latitude=50.64624506666667, longitude=-4.7369719, accuracy=9m at :1172318357
10-24 21:11:20.207: I/Web Console(19755): Position updated: latitude=50.646218950000005, longitude=-4.7369769999999995, accuracy=9m at :1172318357
10-24 21:11:21.207: I/Web Console(19755): Position updated: latitude=50.64621395000001, longitude=-4.7369834, accuracy=9m at :1172318357
10-24 21:11:22.207: I/Web Console(19755): Position updated: latitude=50.646217416666666, longitude=-4.736986783333333, accuracy=12m at :1172318357
10-24 21:11:23.207: I/Web Console(19755): Position updated: latitude=50.646211816666664, longitude=-4.7369768500000005, accuracy=12m at :1172318357
10-24 21:11:24.707: I/Web Console(19755): Position updated: latitude=50.646210450000005, longitude=-4.736994233333333, accuracy=12m at :1172318357
10-24 21:11:25.707: I/Web Console(19755): Position updated: latitude=50.64621571666667, longitude=-4.736988583333333, accuracy=12m at :1172318357
10-24 21:11:26.707: I/Web Console(19755): Position updated: latitude=50.646216466666665, longitude=-4.7370037, accuracy=12m at :1172318357
10-24 21:11:27.707: I/Web Console(19755): Position updated: latitude=50.64621723333333, longitude=-4.737019266666667, accuracy=20m at :1172318357
10-24 21:11:28.717: I/Web Console(19755): Position updated: latitude=50.64621738333334, longitude=-4.737011333333333, accuracy=12m at :1172318357
10-24 21:11:29.717: I/Web Console(19755): Position updated: latitude=50.64621768333333, longitude=-4.7370187, accuracy=18m at :1172318357
10-24 21:11:30.727: I/Web Console(19755): Position updated: latitude=50.64621798333333, longitude=-4.737026116666667, accuracy=18m at :1172318357

I did solve the problem adding this line to manifest: 我确实解决了将此行添加到清单的问题:

<uses-permission android:name="android.permission.INTERNET" />

By default, CLI commands does not add this line to manifest. 默认情况下,CLI命令不会将此行添加到清单。

You must give permission to the app to get the position using wifi and cellphone antennas 你必须允许该应用程序使用wifi和手机天线获得该位置

I was testing using this line but there was no difference: 我正在测试使用这条线,但没有区别:

<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />

I already changed the time to get new position in file GPSListener.java located in app/src/org.apache.cordova.geolocation at this line: 我已经更改了在此行的app / src / org.apache.cordova.geolocation中的文件GPSListener.java中获取新位置的时间:

this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 0, this);

I did changed 60000 for 2000, but there was no difference. 我确实在2000年改变了60000,但没有区别。

My Device is i9300 with Android 4.3 我的设备是i9300与Android 4.3

I hope this info can help somebody! 我希望这些信息可以帮助别人!

Greetings! 问候!

First make sure that your code is bug free ! 首先确保您的代码没有错误! If you are sure then you need to do one more thing ! 如果你确定那么你还需要做一件事! Just change the security settings of your Android device. 只需更改Android设备的安全设置即可。 Go to Settings -> Security -> Check the "Unknown sources" checkbox in order to allow your apk to be run without any problem. 转到设置 - >安全性 - >选中“未知来源”复选框,以允许您的apk运行没有任何问题。

Hope it works as it did for me ! 希望它能像我一样工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM