AS3/Flash Geolocation API

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).

Advertisements

About killerspaz

I'm a developer that loves to tinker with cutting edge technology. I have recently been playing with the Flash Platform (AS3/Flex), Android (custom roms, replacement apps, scripting), and looking at opportunities in the mobile markets.

Posted on 01.29.2011, in Flash and tagged , , , , , , , , . Bookmark the permalink. 1 Comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: