Search code examples
geolocationcordovaonerror

Android Geolocation onError Phonegap


Maybe somebody can shed some light into this.

I am building a mobile app for Android using Phonegap that uses geolocation and FourSquare's API. I wanted to allow the app to run offline, so I resorted to use the onError method in the getCurrentPosition method. Mystery is, this works only sometimes.

I have tried all possible possibilities, including testing with the device's Wireless and/or GPS on/off, and the result is the same: sometimes the onError method gets called, whereas sometimes it doesn't... The app works just fine when it is online.

I have included the essential part of the javascriptcode, please let me know if you detect any fundamental errors or have any suggestions. I can also upload the rest... Thanks in advance.

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


// Cordova is ready 
function onDeviceReady() {
   navigator.geolocation.getCurrentPosition(onSuccess, onError);
}


// onSuccess Geolocation 
function onSuccess(position) {


$('#buttonNo1').click(checkLocation);


}

function checkLocation() {

    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;

     var url2 = 'https://api.foursquare.com/v2/venues/search?ll='+latitude+','+ longitude + '&radius=10&oauth_token=FZGTD3IDTVJVJPFDARSSMZGVOTMK2ZOUPLHHYZKKN1JYJ11D&v=20130627&callback=?';

    $.get(url2, function(data) {

            venue0 =  data.response.venues[0].categories[0].name;
            venue1 =  data.response.venues[1].categories[0].name;
            venue2 =  data.response.venues[2].categories[0].name;
            populateHTML(venue0, venue1, venue2);

            }, "jsonp");

}


function onError(error) {

    populateHTMLManually();

}

Solution

  • There's no reason that you should need an internet connection in order to update your position. Even if both GPS and Wifi are turned off on the device, it should be possible to use cell triangulation to work out an approximate location.

    If you want the Android device to make use of the GPS receiver (if the device has one), you must use the enableHighAccuracy option when requesting the position:

    navigator.geolocation.getCurrentPosition(onSuccess, onError, {enableHighAccuracy: true});
    

    With regard to the errors while offline, it's probably because the default timeout value is expiring before a position update is received (see the phonegap Geolocation API documentation. Try increasing both the timeout and maxiumumAge:

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

    Though I'm not sure how you're intended user experience should be, you may want to consider using watchPosition instead of getCurrentPosition. This sets up a watcher that will call the onSuccess function each time the device receives a position update, be it from the GPS receiver, WiFi or by cell triangulation.