I'm using Phonegap.Geolocation to get the current position of the user.
And I've found this example in the documentation from the official website.
However, even thought the onDevice ready is executed, neither success nor error function callback is executed. And I'm really confused by this.
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
//
function onDeviceReady() {
alert('deviceready');
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
// onSuccess Geolocation
//
function onSuccess(position) {
var element = document.getElementById('geolocation');
element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +
'Longitude: ' + position.coords.longitude + '<br />' +
'Altitude: ' + position.coords.altitude + '<br />' +
'Accuracy: ' + position.coords.accuracy + '<br />' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '<br />' +
'Heading: ' + position.coords.heading + '<br />' +
'Speed: ' + position.coords.speed + '<br />' +
'Timestamp: ' + position.timestamp + '<br />';
}
// onError Callback receives a PositionError object
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
Make sure youryou installed the geolocation plugin (run phonegap local plugin add org.apache.cordova.geolocation
), and that GPS is enabled in the device.
Add a timeout and set enableHighAccuracy:
navigator.geolocation.getCurrentPosition(onSuccess, onError, {timeout: 10000, enableHighAccuracy: true});
In certain emulators you need to set enableHighAccuracy to false, so try that if still doesn't work.
If you are using an Android emulator, it doesn’t read GPS values, so we need to send them via command line. We need to start a telnet session in the port that the emulator is running (you can check the port in the emulator window title, the number at the beginning, in my case 5554):
telnet localhost 5554
And then run the command
geo fix -122.4 37.78
If you close the app you need to re-send the geolocation, so if it doesn’t work, just run the geo fix
command just after opening the app, before the timeout event fires.
Actually it is working, however the location cannot be determined in a short time so neither can it be deemed as success nor fail, and thus no action is performed.
This can be managed by setting a time-out for the callback of GeoLocation
When there is a phonegap feature not working, check if the corresponding permission in the app is set. e.g for internet access, or geoposition on an android-device your AndroidManifest.xml must contain following lines:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Ok. I too struggled with this problem [thats how I got to this page]
But it finally worked. I just made on change:
I changed this line : navigator.geolocation.getCurrentPosition(onSuccess, onError);
to this : navigator.geolocation.getCurrentPosition(onSuccess);
and the whole thing now works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With