Phonegap 地理位置有时在 android 上不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23383750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Phonegap geolocation sometimes not working on android
提问by WuZhonghua
I have using cordova v3.4.1 to build android and ios app, on ios geolocation function is returning very quickly and works fine always,but on android, sometimes it not working.
我使用cordova v3.4.1 来构建android 和ios 应用程序,在ios 上的地理定位功能返回非常快并且始终工作正常,但是在android 上,有时它不起作用。
The strange thing is after I reboot my android phone,it works fine. In more than a few hours geolocation gets the postion very quickly both in wifi and 3G.
奇怪的是在我重新启动我的安卓手机后,它工作正常。在几个多小时内,地理定位在 wifi 和 3G 中都很快得到了定位。
But after serval hours, I open the app again, the geolocation can not work.Sometimes when I outside, it can get postion with gps satellite,but very slow,often occur timeout error.
但是几个小时后,我再次打开应用程序,地理定位无法工作。有时我在外面时,可以使用gps卫星定位,但速度很慢,经常出现超时错误。
I try to remove the app and reinstall it again,but the problem still exist,unless reboot my android phone.when I reboot my android phone,geolocation function is working fine again in future a few hours.
我尝试删除该应用程序并重新安装它,但问题仍然存在,除非重新启动我的 android 手机。当我重新启动我的 android 手机时,地理定位功能在未来几个小时内再次正常工作。
I have test serval android`s phone such as samsang note2, galaxy4 etc, they all have the same problem.when I reboot it, they can get location very quicly.
我测试了 samsang note2、galaxy4 等 serval android 手机,它们都有相同的问题。当我重新启动它时,它们可以非常快速地获取位置。
This problem has troubled me for a long time, so somebody help me?
这个问题困扰我很久了,有人帮帮我吗?
here is my code below:
这是我的代码如下:
navigator.geolocation.getCurrentPosition(function(pos){
cb(null,pos)
},function(errMsg){
navigator.geolocation.getCurrentPosition(function(pos){
cb(null,pos)
},function(errMsg){
cb(errMsg)
}, {
enableHighAccuracy: true,
timeout: 60*1000*2,
maximumAge: 1000*60*10
});
}, {
enableHighAccuracy: false,
timeout: 10*1000,
maximumAge: 1000*60*10
});
my config.xml:
我的 config.xml:
<feature name="Geolocation">
<param name="android-package" value="org.apache.cordova.geolocation.GeoBroker" />
</feature>
回答by WuZhonghua
Finally I have solve the problem,now on my android phone, getCurrentPosition
function return location infomation very quickly every time, I never need reboot my phone again!
I'm not sure wether cordova v3.4.1's geoloation plugin has bugs.
终于我解决了这个问题,现在在我的安卓手机上,getCurrentPosition
功能每次都很快返回位置信息,我再也不需要重启手机了!我不确定cordova v3.4.1 的geoloation 插件是否有错误。
The reason to occur this problem is we always to use browser's navigator.geolocation.getCurrentPosition
function to locate the postion,so we must let app use cordova native implementation instead of html5.
出现这个问题的原因是我们总是使用浏览器的navigator.geolocation.getCurrentPosition
功能来定位位置,所以我们必须让app使用cordova原生实现而不是html5。
Here is the the solution with cordova v3.4.1, hope it can help you,in fact it has resolved my question.
这是cordova v3.4.1的解决方案,希望能帮到你,其实已经解决了我的问题。
solve step:
解决步骤:
1.remove the org.apache.cordova.geolocation
1.删除org.apache.cordova.geolocation
$ cordova plugin rm org.apache.cordova.geolocation
2.reinstall a new org.apache.cordova.geolocation plugin
2.重新安装一个新的org.apache.cordova.geolocation插件
$ cordova plugin add org.apache.cordova.geolocation
3.copy the plugin to e:\
3.将插件复制到e:\
enter the plugin folder,such as ‘YOUR_APP_FOLDER\plugins\'
进入插件文件夹,如'YOUR_APP_FOLDER\plugins\'
copy the hole org.apache.cordova.geolocation
to E:\org.apache.cordova.geolocation
将孔复制org.apache.cordova.geolocation
到E:\org.apache.cordova.geolocation
4.remove the plugin again
4.再次删除插件
$ cordova plugin rm org.apache.cordova.geolocation
5.edit the plugin.xml
in E:\org.apache.cordova.geolocation\plugin.xml
5.edit的plugin.xml
中E:\org.apache.cordova.geolocation\plugin.xml
edit the plugin.xml file like below:
编辑 plugin.xml 文件,如下所示:
... ...
<name>Geolocation</name>
<description>Cordova Geolocation Plugin</description>
<license>Apache 2.0</license>
<keywords>cordova,geolocation</keywords>
<repo>https://git-wip-us.apache.org/repos/asf/cordova-plugin-geolocation.git</repo>
<issue>https://issues.apache.org/jira/browse/CB/component/12320638</issue>
<!-- android Code Comments
<platform name="android">
<config-file target="AndroidManifest.xml" parent="/*">
<uses-permissionandroid 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" />
</config-file>
</platform>
-->
<!-- amazon-fireos change to android-->
<platform name="android">
<js-module src="www/Coordinates.js" name="Coordinates">
<clobbers target="Coordinates" />
</js-module>
<js-module src="www/PositionError.js" name="PositionError">
<clobbers target="PositionError" />
</js-module>
<js-module src="www/Position.js" name="Position">
<clobbers target="Position" />
</js-module>
<js-module src="www/geolocation.js" name="geolocation">
<clobbers target="navigator.geolocation" />
</js-module>
<config-file target="res/xml/config.xml" parent="/*">
<feature name="Geolocation">
<param name="android-package" value="org.apache.cordova.geolocation.GeoBroker"/>
</feature>
</config-file>
<config-file target="AndroidManifest.xml" parent="/*">
<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" />
</config-file>
<source-file src="src/amazon/GeoBroker.java" target-dir="src/org/apache/cordova/geolocation" />
<source-file src="src/amazon/GPSListener.java" target-dir="src/org/apache/cordova/geolocation" />
<source-file src="src/amazon/NetworkListener.java" target-dir="src/org/apache/cordova/geolocation" />
<source-file src="src/amazon/CordovaLocationListener.java" target-dir="src/org/apache/cordova/geolocation" />
</platform>
<!-- ios -->
<platform name="ios">
... ...
6.add plugin again
6.再次添加插件
$ cordova plugin add E:\org.apache.cordova.geolocation
7、run build commond
7、运行build commond
$ cordova build android
8.check if it work fine
8.检查它是否工作正常
check YOUR_APP\plugins\android.json
must include:
检查YOUR_APP\plugins\android.json
必须包括:
{
"xml": "<feature name=\"Geolocation\"><param name=\"android-package\" value=\"org.apache.cordova.geolocation.GeoBroker\" /></feature>",
"count": 1
},
... ...
{
"xml": "<uses-permission android:name=\"android.permission.ACCESS_COARSE_LOCATION\" />",
"count": 4
},
{
"xml": "<uses-permission android:name=\"android.permission.ACCESS_FINE_LOCATION\" />",
"count": 4
},
{
"xml": "<uses-permission android:name=\"android.permission.ACCESS_LOCATION_EXTRA_COMMANDS\" />",
"count": 1
}
check YOUR_APP\platforms\android\AndroidManifest.xml
must include:
检查YOUR_APP\platforms\android\AndroidManifest.xml
必须包括:
<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_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
check YOUR_APP\platforms\android\res\xml\config.xml
must include:
检查YOUR_APP\platforms\android\res\xml\config.xml
必须包括:
<feature name="Geolocation">
<param name="android-package" value="org.apache.cordova.geolocation.GeoBroker" />
</feature>
9.run the app
9.运行应用程序
write the geelocation code to get lat and lng:
编写地理定位代码以获取 lat 和 lng:
navigator.geolocation.getCurrentPosition(function(pos){
alert('suc')
alert(JSON.stringify(pos))
},function(errMsg){
alert(JSON.stringify(errMsg))
navigator.geolocation.getCurrentPosition(function(pos){
alert('suc')
alert(JSON.stringify(pos))
},function(errMsg){
alert(JSON.stringify(errMsg))
}, {
enableHighAccuracy: true,
timeout: 60*1000*2,
maximumAge: 1000*60*10
});
}, {
enableHighAccuracy: false,
timeout: 60*1000,
maximumAge: 1000*60*10
});
});
if your phone support use wifi/3G locate then we will get location immediately,else you will see the errMsg timeout
and then geolocation
will use gps to locate postion with native implement.
如果您的手机支持使用 wifi/3G 定位,那么我们将立即获取位置,否则您将看到errMsg timeout
然后geolocation
使用本机工具使用 GPS 定位位置。
now we get the lat and lng, and never need reboot the android phone agian,thank goodness.
现在我们得到了 lat 和 lng,再也不需要重启安卓手机了,谢天谢地。
if you still have the problem,try to install the plugin to clean the webview cache:
如果问题仍然存在,请尝试安装插件来清理 webview 缓存:
回答by user2147549
on cordova v5.3.3,you need to do this:
在cordova v5.3.3上,你需要这样做:
1、cordova plugin remove cordova-plugin-geolocation
1、cordova插件去掉cordova-plugin-geolocation
2、cordova plugin add cordova-plugin-geolocation
2、cordova插件添加cordova-plugin-geolocation
3、copy your cordova-plugin-geolocation directory in your plguins dir to e:\
3、将你的plguins目录下的cordova-plugin-geolocation目录复制到e:\
4、edit plugin.xml , add this:
4、编辑 plugin.xml ,添加:
<js-module src="www/Coordinates.js" name="Coordinates">
<clobbers target="Coordinates" />
</js-module>
<js-module src="www/PositionError.js" name="PositionError">
<clobbers target="PositionError" />
</js-module>
<js-module src="www/Position.js" name="Position">
<clobbers target="Position" />
</js-module>
<js-module src="www/geolocation.js" name="geolocation">
<clobbers target="navigator.geolocation" />
</js-module>
<config-file target="res/xml/config.xml" parent="/*">
<feature name="Geolocation">
<param name="android-package" value="org.apache.cordova.geolocation.GeoBroker"/>
</feature>
</config-file>
<config-file target="AndroidManifest.xml" parent="/*">
<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" />
</config-file>
5、cordova plugin remove cordova-plugin-geolocation
5、cordova插件去掉cordova-plugin-geolocation
6、cordova plugin add e:\cordova-plugin-geolocation
6、cordova插件添加e:\cordova-plugin-geolocation
7、cordova run android now, I got gps position well. thanks for WuZhonghua.
7、cordova现在运行android,我得到了很好的gps位置。谢谢吴忠华。