Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap Geolocation not working

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');
    }
like image 674
SolessChong Avatar asked May 31 '13 20:05

SolessChong


4 Answers

  1. Make sure youryou installed the geolocation plugin (run phonegap local plugin add org.apache.cordova.geolocation), and that GPS is enabled in the device.

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

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

like image 183
Jesús Carrera Avatar answered Nov 17 '22 06:11

Jesús Carrera


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

like image 36
SolessChong Avatar answered Nov 17 '22 06:11

SolessChong


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" />
like image 5
Karl Avatar answered Nov 17 '22 08:11

Karl


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.

like image 1
user2413571 Avatar answered Nov 17 '22 06:11

user2413571