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.
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.
Here is the the solution with cordova v3.4.1, hope it can help you,in fact it has resolved my question.
solve step:
1.remove the org.apache.cordova.geolocation
$ cordova plugin rm org.apache.cordova.geolocation
2.reinstall a new org.apache.cordova.geolocation plugin
$ cordova plugin add org.apache.cordova.geolocation
3.copy the plugin to e:
enter the plugin folder,such as ‘YOUR_APP_FOLDERplugins’
copy the hole org.apache.cordova.geolocation
to E:org.apache.cordova.geolocation
4.remove the plugin again
$ cordova plugin rm org.apache.cordova.geolocation
5.edit the plugin.xml
in E:org.apache.cordova.geolocationplugin.xml
edit the plugin.xml file like below:
... ...
<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
$ cordova plugin add E:org.apache.cordova.geolocation
7、run build commond
$ cordova build android
8.check if it work fine
check YOUR_APPpluginsandroid.json
must include:
{
"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_APPplatformsandroidAndroidManifest.xml
must include:
<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_APPplatformsandroid
esxmlconfig.xml
must include:
<feature name="Geolocation">
<param name="android-package" value="org.apache.cordova.geolocation.GeoBroker" />
</feature>
9.run the app
write the geelocation code to get lat and 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.
now we get the lat and lng, and never need reboot the android phone agian,thank goodness.
if you still have the problem,try to install the plugin to clean the webview cache:
https://github.com/moderna/cordova-plugin-cache