user2331670
user2331670

Reputation: 335

android geolocation using phonegap code: 3 error

I'm trying to use geolocation using PhoneGap API doc but getting below error message,

Alert code:3 message: Timeout expired

My code is,

    <script>

// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);

var watchID = null;

// Cordova is ready
//
function onDeviceReady() {
    // Throw an error if no update is received every 30 seconds
    var options = { timeout: 10000 };
    watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
}

// onSuccess Geolocation
//
function onSuccess(position) {
    var element = document.getElementById('geolocation');
    element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +
                    'Longitude: ' + position.coords.longitude + '<br />' +
                    '<hr />' + element.innerHTML;
}

// onError Callback receives a PositionError object
//
function onError(error) {
    alert('code: ' + error.code + '\n' +
      'message: ' + error.message + '\n');
}

Please advice me what I am missing.

Upvotes: 6

Views: 10244

Answers (6)

Mikhail.root
Mikhail.root

Reputation: 832

I've tried cordova-plugin-geolocation with ngCordova but it always returned me Timeout error, meaning it can't get device's position in my even 5 minutes set timeout, so the final solution for Android 5.1 at least: i've added permissions to AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.NETWORK_ACCESS" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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" />

it wasn't working without ACCESS_WIFI_STATE permission.

Now it works even without a sim card, with wifi connected and location enabled.

In case someone will find it helpfull.

Upvotes: 0

Caleb Pitman
Caleb Pitman

Reputation: 1165

I solved this problem for an HTC M8 running the latest Android (physical device) by not specifying maximumAge and timeout.

navigator.geolocation.getCurrentPosition(onSuccess, onError, {
  // maximumAge: 100,
  // timeout: 2000,
  enableHighAccuracy: true
});

On iPhone 5 it worked fine, but the Android only returned errors. Now both work without problems. I'll note that I had even tried 1 hour maximumAge and high timeouts, and that didn't work. This is the only solution that worked for all scenarios.

Upvotes: 2

Igor Parra
Igor Parra

Reputation: 10348

Another reason for this malfuntion (it was my case) is to install an apk manually and do not grant related permission to the app. For example:

Network communication: view Wi-Fi state, create Bluetooth connections, full Internet access, view network state

Your location: access extra location provider commands, fine (GPS) location, mock location sources for testing, coarse (network-based) location

enter image description here

+info about permissions: http://developer.android.com/guide/topics/security/permissions.html

Upvotes: 0

Jes&#250;s Carrera
Jes&#250;s Carrera

Reputation: 11425

Assuming you are getting this problem in an Android emulator:

  1. 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.

  2. In Android, the emulator don’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.

If you are using a real Android device, make sure it has GPS enabled, and that you installed the geolocation plugin (run phonegap local plugin add org.apache.cordova.geolocation)

Upvotes: 4

Obi
Obi

Reputation: 3091

I believe this is a relatively common problem especially with Android devices. Some of the popular solution I have come across include

  1. Do not specify the error callBack: For some really odd reason this works, your only problem will arise if GPS isn't enabled. I wouldn't advise this in a production app though.

  2. Specify enableHighAccuracy: true in your options. This seems to work but not on its own, see #3 below

  3. Specify maximumAge in options: I use this one and set it very high to maybe 1hour. What it does is that even if there is no GPS currently, cordova will give you the last known coordinates captured within the last hour, so this really depends on how accurate you want your coordinates to be with respect to time.

I usually use a combination of #2 and #3 and this works reasonably for Phonegap 3.x.

As an side, you could include an option to start an Intent via a plugin for the device's settings in your error callback so that the user can enable their GPS if disabled (Like google maps on the phone does when you use it for navigation).

I hope this helps.

Upvotes: 4

Mister Smith
Mister Smith

Reputation: 28168

Phonegap uses the same interface as the Geolocation API, that you can use also in regular web apps without the Phonegap libraries.

From the specs:

TIMEOUT (numeric value 3)
The length of time specified by the timeout property has elapsed before the implementation could successfully acquire a new Position object.

So you could try providing a higher timeout value as parameter. 10 seconds is very short time if your only provider is GPS. In this case, getting a fix could take 40 to 60 seconds even outdoors.

Upvotes: 0

Related Questions