jeudi 16 juin 2016

Geolocation Javascript Cordova Not Triggered

I have a mobile app built with Cordova. On one of the button an onclick event triggers a functions supposed to alert the geolocation. I've installed Cordova geolocation API for the project with cordova plugin add cordova-plugin-geolocation from https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-geolocation/#weather

Then I have the functions according to https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-geolocation/#reference

So basicaly my HTML is

<button onclick="getPOS();">CLICK_ME</button>

And my JS is

function getPOS(){

    alert("ok");

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

    navigator.geolocation.getCurrentPosition(onSuccess, onError, { timeout: 30000 });
}

When I launch this in my firefox browser, it asks if I want to share location. If I choose to share, it shows the location fine.

Next When I build the app with cordova, when the apk installation is complete, it says the app requires Network connection and Location ..... which is normal and it means everything is properly set.

Then what I do is enable my phones GPS and Internet connextion

PROBLEM : upon clicking the link CLICK_ME on my phone, nothing happens though GPS and internet are enabled.

After the time out is expired, I get the following error :

Code: 3 message: Timeout expired

Any idea what's wrong here ?

Aucun commentaire:

Enregistrer un commentaire