I am working with geolocation in phonegap application. I get the location easily when High Accuracy and Battery saving mode is ON. But it gives error while working in GPS. Please help. I am stuck in it.
I applied the timeout upto 60000 milliseconds. But it didnot work even then.
The code is -
function Geolocation(){}
Geolocation.options = {
maximumAge: 60000,
timeout: 15000,
enableHighAccuracy: true
};
Geolocation.errorMessage = 'Unable to get location data. Ensure that the location services are enabled in high accuracy mode. If enabled, try turning off/on location services and open this page again';
Geolocation.update = function() {
navigator.geolocation.getCurrentPosition(Geolocation.updateSuccess, Geolocation.highAccuracyError, Geolocation.options);
}
Geolocation.highAccuracyError = function(err) {
if (err.code == 3) {
var options = Geolocation.options;
options.enableHighAccuracy = false;
options.timeout = 15000;
navigator.geolocation.getCurrentPosition(Geolocation.updateSuccess, Geolocation.lowAccuracyError, options);
}
}
Geolocation.lowAccuracyError = function(err) {
Geolocation.currentLocation = null;
}
Geolocation.updateSuccess = function(position) {
Geolocation.currentLocation = {};
Geolocation.currentLocation.latitude = position.coords.latitude;
Geolocation.currentLocation.longitude = position.coords.longitude;
}
Thanks in advance
@megha,
with the GPS there are two issues.
1) On Android, you must turn on all the location services, including GPS satellites, Google's location service, and Quick GPS. Otherwise, you will get that timeout error.
I think google broke it on purpose. It did not used to be that way.
2) When using GPS, you must be in motion - walking around for at least 20 feet (even if it is in a circle), And you must have line-of-site of at least 3 satellites. Most people get around this last problem by being in an open area -- like a soccer field or parking lot.
Jesse
You should try using watchPosition()
instead of getCurrentPosition()
.
getCurrentPosition()
makes a single request for the device position at that current point in time, so the position timeout may occur before the GPS hardware on the device has had a chance to get a position fix.
I'd suggest using watchPosition()
instead to setup a watcher which will call the success function each time the OS receives a location update from the GPS hardware.
So, based on your current code, something like:
function Geolocation(){}
Geolocation.watchID;
Geolocation.minAccuracyInMetres = 50;
Geolocation.options = {
maximumAge: 60000,
timeout: 15000,
enableHighAccuracy: true
};
Geolocation.errorMessage = 'Unable to get location data. Ensure that the location services are enabled in high accuracy mode. If enabled, try turning off/on location services and open this page again';
Geolocation.update = function() {
Geolocation.watchID = navigator.geolocation.watchPosition(Geolocation.updateSuccess, Geolocation.highAccuracyError, Geolocation.options);
}
Geolocation.highAccuracyError = function(err) {
if (err.code == 3) {
var options = Geolocation.options;
options.enableHighAccuracy = false;
options.timeout = 15000;
navigator.geolocation.getCurrentPosition(Geolocation.updateSuccess, Geolocation.lowAccuracyError, options);
}
}
Geolocation.lowAccuracyError = function(err) {
Geolocation.currentLocation = null;
}
Geolocation.updateSuccess = function(position) {
// Reject if accuracy is not sufficient
if(position.coords.accuracy > Geolocation.minAccuracyInMetres){
return;
}
// If only single position is required, clear watcher
navigator.geolocation.clearWatch(Geolocation.watchID);
Geolocation.currentLocation = {};
Geolocation.currentLocation.latitude = position.coords.latitude;
Geolocation.currentLocation.longitude = position.coords.longitude;
}
It is entirely possible that your phone is simply not able to get it's location based on GPS hardware alone. For example, if you are underground your phone will not be able to connect to the GPS satellites. And you don't need to be underground for that to be the case. From what you have said I think Cordova is correctly reporting to you that the phone cannot determine it's location.