Search

 

July 2010
S M T W T F S
« Jun   Sep »
 123
45678910
11121314151617
18192021222324
25262728293031

Tags

Archives


« | Main | »

AIR On Android: Geolocation

By Rich Tretola | July 22, 2010
26,626 views

So July has been a quiet month for this blog, my apologies. I have been spending some time digging into the AIR on Android documentation. Working with the Flash CS5 IDE has been like going in a time machine. It has been quite a while since I have build any Flash Player based application with anything other than the Flex Framework. I would assume that as soon as this becomes a viable option, I will again return to relying on the framework.

My time has been spent working up some proof of concept stuff for an upcoming project. I have been exploring the flash.sensors.Geolocation class for interacting with the devise’s GPS and the flash.media.CameraRoll class for browsing the image library.

First the Geolocation class, I’ll discuss the CameraRoll in a future post. It is very simple to use. In the example below, I have two text fields on my Flash CS5 stage called geoStatus and geoResponse.

Let’s discuss the code below. The constructor makes a call to super() and locate(). The locate() method creates a new instance of the Geolocation class, sets the interval to 100 milliseconds, and adds an event listener to handle the updates that are generated every 100 milliseconds. Since the speed is returned in meters per second, the handleLocationRequest() method creates two variables for miles per hour and kilometers per hour and does the math. Next the text fields are updated to show the last update time and the data returned.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package
{
    import flash.display.Sprite;
    import flash.sensors.Geolocation;
    import flash.events.GeolocationEvent;
   
    public class Geo extends Sprite {
        private var geoLocation:Geolocation;    
        public function Geo() {
            super();   
            locate();
        }

        private function locate():void  {
            if(Geolocation.isSupported==true){
                geoLocation = new Geolocation();
                geoLocation.setRequestedUpdateInterval(100);
                geoLocation.addEventListener(GeolocationEvent.UPDATE, handleLocationRequest);
            } else {
                geoStatus.text = "Geolocation feature not supported";
            }
        }
       
        private function handleLocationRequest(event:GeolocationEvent):void {
            var mph:Number = event.speed*2.23693629;
            var kph:Number = event.speed*3.6;
            geoStatus.text = "Updated: " + new Date().toTimeString();
            geoResponse.text = "latitude: " + event.latitude.toString() + "\n"
                + "longitude: " + event.longitude.toString() + "\n"
                + "altitude: " + event.altitude.toString()  + "\n"
                + "speed: " + event.speed.toString()  + "\n"
                + "speed: " + mph.toString()  + " MPH \n"
                + "speed: " + kph.toString()  + " KPH \n"
                + "heading: " + event.heading.toString()  + "\n"
                + "horizontal accuracy: " + event.horizontalAccuracy.toString()  + "\n"
                + "vertical accuracy: " + event.verticalAccuracy.toString()
        }
    }
}

One more thing that is required is the addition of the android permission to allow access to GPS within the application config file.

1
2
3
4
5
6
7
8
9
<android>
        <manifestAdditions>
            <manifest>
                <data><![CDATA[
                <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
                ]]></data>
            </manifest>
        </manifestAdditions>
    </android>

The results can be see in the screen shot below:

geolocation

Topics: Adobe AIR, android | 16 Comments »

16 Responses to “AIR On Android: Geolocation”

  1. Tweets that mention AIR On Android – Geolocation | EverythingFlex: Flex & AIR -- Topsy.com Says:
    July 22nd, 2010 at 8:44 am

    [...] This post was mentioned on Twitter by Rich Tretola, Jim Armstrong and Carsten Schuetz, gabrielflores. gabrielflores said: #AIR On #Android – Geolocation http://icio.us/ma0fwl [...]

  2. Rob McKeown Says:
    July 22nd, 2010 at 9:56 am

    When you say to add that configuration to the application’s config, do you mean the MyApp-app.xml file? If so, does it matter where in the file you put it?

    Reply to this comment

    Rich Tretola Reply:

    Yes, the android node needs to be within the root application node at the same level as the id, versionNumber, and description nodes.

    Reply to this comment

  3. Pradeek Says:
    July 22nd, 2010 at 10:07 am

    Any thoughts on if we’ll get Geolocation in AIR for desktop?

    Reply to this comment

    Rich Tretola Reply:

    How many desktop machines have GPS? You can do geolocation on desktop now by ipAddress location (which could be way off in shared ip situations) but without a GPS, you really couldn’t get coordinates.

    Reply to this comment

    Pradeek Reply:

    I actually meant implementing the HTML5 spec Geolocation API in AIR. Any thoughts on that?

    Reply to this comment

  4. Adobe Flex Tutorial Says:
    July 22nd, 2010 at 11:39 am

    Nice article, i wrote 2 blog posts about it, one just about getting the GPS infos and the other ones to display it on Google Maps.
    It’s in french but in case you are interested, here are the links:
    http://www.flex-tutorial.fr/2010/07/12/air-android-recuperer-la-geolocalisation-de-lutilisateur-dans-une-application-air/
    http://www.flex-tutorial.fr/2010/07/12/air-android-visualiser-la-geolocalisation-gps-sur-google-maps-avec-air/

    Fabien
    http://www.flex-tutorial.fr

    Reply to this comment

  5. replica watch Says:
    August 5th, 2010 at 5:04 am

    You can do geolocation on desktop now by ipAddress location (which could be way off in shared ip situations) but without a GPS, you really couldn’t get coordinates.

    Reply to this comment

  6. Sidney de Koning Says:
    August 13th, 2010 at 7:53 am

    Hi Rich,

    Not so much a comment / question about the post, but more on the code used :)
    Is there any specific reason why you chose to lowercase your classname and constructor?

    Take care,

    Sidney de Koning

    Reply to this comment

    Rich Tretola Reply:

    No, actually that is a mistake and a bad practice. It should not be lower case.

    Reply to this comment

  7. AIR on iPhone: Geolocation | EverythingFlex: Flex & AIR Says:
    October 20th, 2010 at 7:35 am

    [...] first created this application using Flash CS5 back in July to try out the geo location features of AIR on [...]

  8. Phillip Shakesby Says:
    October 31st, 2010 at 9:45 am

    Hi I have been attempting to complete your tutorial but keep getting an error message:
    Scene 1, Layer ‘actions’, Frame 1, Line 2 1037: Packages cannot be nested.
    please can you help

    Phillip Shakesby

    Reply to this comment

  9. Diogo Coelho Says:
    January 3rd, 2011 at 7:11 am

    Hi :) .
    I just got an iPhone and since I’m a flash/flex developer this post means great news to me :) .

    By the way, my friends told me that I need a mac in order to have my app running in the iPhone. Is there any other way?
    I don’t have a mac so this is could be a problem :S.

    Thanks.
    Diogo Coelho

    Reply to this comment

    Rich Tretola Reply:

    No, you shouldn’t need a Mac to compile, but you may to actually create the provisioning certificates.

    Reply to this comment

  10. Terry Collinson Says:
    April 13th, 2011 at 5:10 am

    First of all thanks. I am a complete newby and these articles are great. I am using Burrito now – does the code need amending in any way? Thanks

    Reply to this comment

    Rich Tretola Reply:

    There are some changes between AIR 2.5 and AIR 2.6 for mobile applications.

    Reply to this comment

Comments