I was playing with the Geolocation API, and have been having the hardest time getting it working. It would activate the GPS on my device (a Droid2), but would never trigger any updates and hit my breakpoint.

I knew this had to be something I was doing, because I’ve seen it working, have examples of working source, and can’t get it to work on my device…

Hitting my head against the API

I think to myself, “Self – maybe this is one of those *rare* times FlashBuilder is acting up, let me trace something…” Nada. Zip. Zilch.
Interesting thing of note here, the GPS icon on Android did happen to show up, but it wasn’t animating. Typically when a fix has been established and being tracked, it animates to indicate activity – but not now.

After trying 3 or 4 different ways based off (I did change it up some, admittedly) how other people were doing it, I decided to copy the Tour de Flex Mobile source and see what I could figure out. I had to tweak it some, but sure enough it was working.

Y U NO WORK?!

I compare mine vs theirs, and really aside from my different approach it was essentially the same, save for a few extra listeners.

When reducing the amount of things going on, it started working. I was surprised – What was I doing that was causing issues?!? I’m frustrated… I’m annoyed… I’m … yeah I already said frustrated.
Slowly, 1 by 1, I started adding things back in, until I found the culprit…

It turns out when I add a listener for Event.DEACTIVATE, updates NEVER trigger… I’ve left it sitting there for 10 minutes once, nothing – but launch native GoogleMaps, and it finds me lickity split.
I felt listening to this event is important to disable the GPS fix, to avoid draining unnecessary battery drain.

The Message

Just want to give a heads up to anyone else attempting this – do NOT add the Event.DEACTIVATE listener to the Geolocation object.
Instead, listen to the NativeApplication.nativeApplication (I’m sure the Stage or container displaying GPS info would work as well) for the same event name, and you should be good!

Bonus!

I created this little utility to save me time from having to set a bunch of values. With this, you are ensured that if the application deactivates, so does GPS. You also can stop it manually, for example if you only need it on a particular view.

Protip – the toDebugString() method is great for traces!

UPDATE: In testing the utility more now that I have hardware, I opted to add a Time To Yield (TTY) to the source. This will give up trying to get a fix after the TTY has expired, the default being 45 seconds (or 45,000 milliseconds).