Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

iPhone Geolocation doesn't work second time

I am writing an AR section for an iPhone app and the first time it runs it works great, but if I exit out of the screen (not the app) and then return to the AR screen the geolocation eventlistener that I add again does not ever get called.

Titanium.Geolocation.addEventListener('heading',goHeading);

I notice the same happens in KitchenSink in the AR demo screen, so it's not just me…

Someone had this problem with Android (http://developer.appcelerator.com/question/43171/android-titaniumgeolocation-wont-run-again-after-pressing-the-arrow-button) - is there a fix for the iPhone SKD as well?

— asked September 23rd 2010 by Paul Burrowes
  • geolocation
  • iphone
0 Comments

6 Answers

  • I have the exact same problem with the location listener (among numerous other problems).

    If I exit the page and go back again it never starts up. Leaving the app and going back in again, it starts up fine.

    Did you ever sort this out?

    — answered October 10th 2010 by Leigh Kayley
    permalink
    0 Comments
  • Have you tried doing a Titanium.Geolocation.removeEventListener('heading'); when you 'close/blur' the window? Then start it when you 'open/focus' it?

    — answered October 10th 2010 by Kosso
    permalink
    0 Comments
  • Same problem here (iPhone) : my app shows the email dialog, and once closed, hop, no more heading. Even removing and setting back the event listener does not bring it to life.

    — answered November 8th 2010 by Christophe Soudron
    permalink
    0 Comments
  • Removing and resetting the listener works for me (SDK 1.7.2):

    Ti.Geolocation.distanceFilter = 15;
    
    var locationCallback = function(e) {
        Ti.Geolocation.removeEventListener('location', locationCallback);
        Ti.Geolocation.addEventListener('location', locationCallback);
    
        hiApp.location = e.coords;
    }
    
    Ti.Geolocation.addEventListener('location', locationCallback);
    
    — answered August 30th 2011 by Kristof Gruber
    permalink
    0 Comments
  • The Problem still exists, anyone has found a workaround for that? Im' on 1.7.5 and 1.7.6.20111206

    — answered December 7th 2011 by Konstantin Filtschew
    permalink
    0 Comments
  • I've fixed it. If you remove the eventListener and there is no event listener registered, the framework stops working and you will never be able to register an event listener for location again. The only chance is to restart the app or program it the right way as appcelerator expects it:

    var locationCallbackEnabled = false;
    
    var locationCallback = function(e) {
        Ti.API.info('coords: '+e.coords);
    }
    
    // If you register one, you have to remove one
    Ti.Geolocation.addEventListener('location', locationCallback);
    // save the information, that an event listener is registered
    locationCallbackEnabled = true;
    
    
    var removelocationCallback = function() {
    
        /* ATTENTION: This is the most important thing. 
          * Check if the listener is added or not. 
             * If you don't do this, you will not be able to 
             * add an event listener again!
         */
        if (locationCallbackEnabled) {
            Ti.Geolocation.removeEventListener('location', locationCallback);
            // save the information, that
            locationCallbackEnabled = false;
        }
    }
    
    // remove the event listener after 20 seconds
    setTimeout(removelocationCallback, 1000*20);
    
    // if you do this again, it is now save because
    // locationCallbackEnabled is set to false
    setTimeout(removelocationCallback, 1000*25);
    
    — answered December 7th 2011 by Konstantin Filtschew
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.