Search code examples
gpstitaniumappceleratortitanium-mobileappcelerator-mobile

Ti.Geolocation callbacks only works when running KitchenSink


I have a problem with Ti.Geolocation that drives me crazy. Only using IOS platform so far. The goal is to get GPS callbacks with highest possible accuracy when I move around with the phone.

The problem is that I have copied most of the code from geolocation.js in KitchenSink, with the relevant part shown below. It looks OK to me, however the behaviour I get is very strange.

I just don't get regular GPS callbacks when I walk around! The compass works fine however and sends me callbacks all the time. I have also tried without subscribing to 'heading' events, but no change in GPS behaviour.

There is only one event that can trigger a GPS-callback with correct data, and that is running KitchenSink! Switching to KitchenSink and back also gives me a callback with accuracy between 5 and 10. If I don't do that, my accuracy can be as high as 1500-2500 (if I get a callback at all, that is).

KitchenSink seems to work fine, but I fail to see what that app does that I do not?!?

I have turned off Wifi in the phone so it wont disturb. This problem is very frustrating and I have spent three days on it now, can someone please help? I have tried compiling against different SDK's too (normally 2.1.1GA but also down to 1.8.2). No change.

if (locationServicesAvailable()) {
    // APPLICATION LOGIC
    ui.init();

    Ti.Geolocation.purpose = "Get Lat/Long of your current position";
    Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST;

    Ti.Geolocation.distanceFilter = 10;
    Ti.Geolocation.frequency = 0; /* as fast as possible */
    Ti.Geolocation.preferredProvider = Ti.Geolocation.PROVIDER_GPS;

    if (Ti.Geolocation.hasCompass) {
        //  TURN OFF ANNOYING COMPASS INTERFERENCE MESSAGE
        Ti.Geolocation.showCalibration = false;
        // SET THE HEADING FILTER (THIS IS IN DEGREES OF ANGLE CHANGE)
        // EVENT WON'T FIRE UNLESS ANGLE CHANGE EXCEEDS THIS VALUE
        Ti.Geolocation.headingFilter = 45;
        /*
        Ti.Geolocation.getCurrentHeading(handleCompass);
        Ti.Geolocation.addEventListener('heading', handleCompass);
        */
    }

    Ti.Geolocation.getCurrentPosition(handlePosition);
    Ti.Geolocation.addEventListener('location', handlePosition);

    ui.refresh.addEventListener('click', function(e) {
        Ti.Geolocation.getCurrentPosition(handlePosition);  
    });
}

Solution

  • OK it seems I have found an answer to my problem. It's as simple as setting accuracy to Ti.Geolocation.ACCURACY_NEAREST_TEN_METERS. I'm using the IOS platform, and I'm located in Sweden (in the countryside) if it matters. Don't know if this solution applies to everyone, but I have seen references to this problem (http://developer.appcelerator.com/question/130596/strange-behavior-of-the-gps) and solution so I know it exists.

    No idea why ACCURACY_BEST doesnt work, but it doesnt for me. I'll test all the other settings too when I get the time but now at least I can continue developing.