I have installed Cordova 5.0.0 with these plugins:
cordova plugin list
cordova-plugin-device 1.0.1-dev "Device"
cordova-plugin-geolocation 1.0.0 "Geolocation"
cordova-plugin-globalization 1.0.0 "Globalization"
cordova-plugin-inappbrowser 1.0.1-dev "InAppBrowser"
cordova-plugin-network-information 1.0.0 "Network Information"
cordova-plugin-whitelist 1.0.1-dev "Whitelist"
Running this code on any android virtual device:
onDeviceReady: function () {
var onSuccess = function(position) {
alert('Latitude: ' + position.coords.latitude + '\n' +
'Longitude: ' + position.coords.longitude + '\n' +
'Altitude: ' + position.coords.altitude + '\n' +
'Accuracy: ' + position.coords.accuracy + '\n' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '\n' +
'Heading: ' + position.coords.heading + '\n' +
'Speed: ' + position.coords.speed + '\n' +
'Timestamp: ' + position.timestamp + '\n');
};
// onError Callback receives a PositionError object
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
var options = { maximumAge: 3000, timeout: 5000, enableHighAccuracy: true };
navigator.geolocation.getCurrentPosition(onSuccess, onError, options);
}
Return:
D/CordovaNetworkManager( 1239): Connection Type: 3g
D/CordovaNetworkManager( 1239): Connection Extra Info: epc.tmobile.com
D/CordovaWebViewImpl( 1239): onPageFinished(file:///android_asset/www/index.html)
D/dalvikvm( 1239): GC_EXTERNAL_ALLOC freed 457K, 49% free 3174K/6151K, external 901K/1038K, paused 3ms
I/InputReader( 843): Device reconfigured: id=0x0, name=qwerty2, display size is now 240x400
I/InputManager-Callbacks( 843): No virtual keys found for device qwerty2.
D/SystemWebChromeClient( 1239): file:///android_asset/www/js/app.js: Line 127 : code: 3
D/SystemWebChromeClient( 1239): message: Timeout expired
Why always returns timeout expired?
It could sound stupid, but, did you activate the "GPS" option in your phone?
I have finally solved all reasons why this happens. This is due to two options: maximumAge
and timeout
. You may find yourself running your app on a device and/or an emulator. In my case, both. The phone GPS was working more than the emulator was. Location services must be turned on on the device. You also need to set a timeout or it goes haywire. For the emulator, you need to send the GPS data to your app via the emulator settings and maximumAge
must be set to a number (In milliseconds) higher than how long ago you sent it.
I also have mine setup so that if an error happens, it tries again by calling a function like this so it will keep trying until it gets it:
function gpsRetry(gpsOptions) {
navigator.geolocation.getCurrentPosition(gpsSuccess, gpsError, gpsOptions);
}
My error function is like this:
function gpsError(error, gpsOptions) {
alert('code: ' + error.code + "\n" +
'message: ' + error.message + "\n");
gpsRetry(gpsOptions);
}
and my success function is like this:
function gpsSuccess(position) {
alert('Latitude: ' + position.coords.latitude + "\n" +
'Longitude: ' + position.coords.longitude + "\n" +
'Altitude: ' + position.coords.altitude + "\n" +
'Accuracy: ' + position.coords.accuracy + "\n" +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + "\n" +
'Heading: ' + position.coords.heading + "\n" +
'Speed: ' + position.coords.speed + "\n" +
'Timestamp: ' + position.timestamp + "\n");
}
Then put it all together with something like this in your onDeviceReady block:
let gpsOptions = {maximumAge: 300000, timeout: 5000, enableHighAccuracy: true};
navigator.geolocation.getCurrentPosition(gpsSuccess, gpsError, gpsOptions);
Once you have all that setup, it should work on your device fine. For the emulator, you need to load your app and then send the GPS coordinates to the app by clicking the ... button in the floating menu and then clicking send:
Once you do that, since your app is always retrying, it should then popup the alert with the GPS coordinates.
I found an answer here, which seemed to work for me:
Simply reboot the device.
Worked for me, hopefully for you too.
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