Titanium Community Questions & Answer Archive

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

GeoLocation Issue

Hey Guys,

So I was in the middle of writing a question when I decided to try one last thing. It didn't work how I wanted to (naturally) but did lead me to an interesting question. I guess I misunderstand how the getCurrentLocation events work…Here's what happened:

– compass.js –

I have a page with a compass, that has getCurrentLocation (and heading) calls. It also has event listeners for those calls that rotate the image on heading calls and changes another field on location calls.

– map.js –

This page has a map, and also has a getCurrentLocation call that shows where a user is on the map.

The pages are visited in this order: the map.js page is a window, and there is a button on it that opens a new window with the compass. NOW, the issue is that the events that I have created in the compass page are firing when I am in the map page, even before I have clicked the compass page. I was under the impression that if an event listener was on a page, it only was active when that page is open, apparently that is not the case? If anyone could enlighten me on how that works it would be great. If the pages are linked somehow, do I need to even call the getCurrentLocation on each page?

Thanks,

Mike

— asked November 13th 2010 by Michael Fogg
  • event
  • geolocation
  • iphone
0 Comments

2 Answers

  • It's been my experience that event listeners continue reporting events even when the page is active. Titanium Mobile apps can have multiple 'execution contexts', each running in its own thread. This blog post was useful in understanding that: http://developer.appcelerator.com/blog/2010/08/execution-contexts.html.

    What I've done, with some success, is to add a custom event to the listener on one page, then put a listener for that on the other page. That way, (for example) when the current location is updated on your map.js, it fires off a message that gets picked up by the compass.js page. Something like this should work:

    On map.js:

    
    Titanium.Geolocation.addEventListener('location',function(e) {
        var newLatitude = e.coords.latitude;
        var newLongitude = e.coords.longitude;
    
        Ti.App.fireEvent('setLocationEvent', {
             latitude: newLatitude,
             longitude: newLongitude
        });            
    });
    

    On compass.js

    Ti.App.addEventListener('setLocationEvent', function(event) {
        var currentLatitude = event.latitude;
        var currentLongitude = event.longitude;
    });
    

    ~~~

    — answered November 30th 2010 by Mark Pemburn
    permalink
    0 Comments
  • "the map.js page is a window, and there is a button on it that opens a new window with the compass"

    Are both within the same file? Do you build the compass window and then just not show it till the end? If so, then I'd expect the behavior you're seeing. The compass window would be constructed in memory and just not visible in such a situation thus I'd expect its listeners to be, uh, listening.

    I think my first attempt would be to move the compass's addEventListener code to the button's event listener so that they're not attached until the compass window is opened. Then, I'd add a removeEventListener to whatever code closes the compass window.

    — answered November 30th 2010 by Tim Poulsen
    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.