Stephane G
Stephane G

Reputation: 71

Cordova android geolocation timeout

I'm getting mad at trying to make geolocation working on Android I followed steps as explained in Cordova documentation pages My cordova version is 3.1.0-0.2.0

$ cordova create myApp com.mymapackage.myApp myApp
$ cordova platform add ios
$ cordova platform add android
$ cordova plugin add org.apache.cordova.geolocation

AndroidManifest contains

<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" />

app/res/xml/config.xml contains

<feature name="Geolocation">
<param name="android-package" value="org.apache.cordova.geolocation.GeoBroker" />
</feature>

I also copied the simple example to tryout geolocation

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

    // device APIs are available
    //
    function onDeviceReady() {
        navigator.geolocation.getCurrentPosition(onSuccess, onError, { timeout: 30000, enableHighAccuracy: true });
    }

    // 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');
    }

and all I can get is a timeout error. I have tried on many different target version/devices with ADT emulator, Genymotion emulator and cannot get anything out of geolocation position. By adding ios platform it was quite straight forward having location information when emulating using xCode.

I'm starting getting desperate with this thing. Any idea that could help?

Upvotes: 2

Views: 2153

Answers (1)

Ortomala Lokni
Ortomala Lokni

Reputation: 62486

There is already issues reported upstream very similar to yours : "Geolocation watchPosition consistently timing out on Android 4.0.4 with Cordova 3.3.0" (issue.apache.org)

Moreover the cordova geolocation plugin is known to have multiple issues and will be deprecated soon[1].

The solution is to use regular browser-based geolocation. The test given by MBillau[2] tries to use geolocation via the plugin and via the browser.

The last option is to go native or to write your own plugin.

[1] https://issues.apache.org/jira/browse/CB-5977

[2] https://github.com/apache/cordova-mobile-spec/blob/master/location/index.html

Upvotes: 1

Related Questions