Phonegap geolocation not working on Android (Nexus device)
I know this question has been asked before, but I've tried all the suggested solutions and nothing works!
I am working on a mobile app built with Phonegap (+ Angular, but I don't think it matters). In this application, I am using a geolocation plugin. Specifically, my config.xml looks like this:
<?xml version="1.0" encoding="UTF-8" ?>
<widget xmlns = "http://www.w3.org/ns/widgets"
xmlns:gap = "http://phonegap.com/ns/1.0"
id = "......"
version = "1.0.0" >
<name>.....</name>
<preference name="permissions" value="none"/>
<preference name="orientation" value="default" />
<preference name="target-device" value="universal" />
<preference name="phonegap-version" value="3.6.3" />
<gap:plugin name="org.apache.cordova.geolocation" />
<gap:plugin name="com.phonegap.plugins.PushPlugin" version="2.4.0" />
<gap:plugin name="org.apache.cordova.network-information" />
<gap:plugin name="org.apache.cordova.statusbar" />
<gap:plugin name="org.apache.cordova.device" />
<gap:plugin name="org.transistorsoft.cordova.background-geolocation"/>
</widget>
There are times when I want to get the current position of the user, so I do it like this:
navigator.geolocation.getCurrentPosition(function (position) {
.....
}, function (error) {}, {
enableHighAccuracy: true,
maximumAge: 30000,
timeout: 10000
});
This works great on iOS devices and it even works on the ripple emulator for Android.
But when I try to run the app on Android and more specifically on Nexus 4 and 5, geolocation doesn't work. It's not that he is giving me an error, he is just trying to get a position with no luck.
However, all other apps that use geolocation as well as geolocation in the browser work fine!
I would also like to point out that I am using Phonegap Build to build my application.
Does anyone know how I can solve this problem? Has anyone had the same problem?
source to share
From your example code, how do you know that no error is thrown when there is an empty error handler function (error) {}
?
getCurrentPosition()
makes one request for device position. If this is called only once, a position timeout may occur before the GPS hardware on the device has a chance to receive a position fix.
I would suggest using watchPosition()
this instead to set up a watcher that will call the success function every time the OS receives a location update. If you only need one position, you can clear the observer, optionally checking to make sure you have a position of acceptable accuracy.
For example:
var minAccuracyInMetres = 50;
var positionWatcher;
function onDeviceReady(){
positionWatcher = navigator.geolocation.watchPosition(
geolocationSuccess,
geolocationError,
{maximumAge:0, timeout: 10000, enableHighAccuracy: true});
}
function geolocationSuccess(position){
// Reject if accuracy is not sufficient
if(position.coords.accuracy > minAccuracyInMetres){
return;
}
// Only single position required so clear watcher
navigator.geolocation.clearWatch(positionWatcher);
// Do something with the user location...
}
function geolocationError(error){
console.warn("Error while retrieving current position. " +
"Error code: " + error.code + ",Message: " + error.message);
}
document.addEventListener("deviceready", onDeviceReady, false);
source to share