Skip to main content
Participant
June 27, 2011
Question

AIR for IOS GeolocationEvent Altitude problem

  • June 27, 2011
  • 1 reply
  • 2158 views

I'm testing a simple geolocation app using the code from this tutorial.

http://www.youtube.com/watch?v=1YRJuBqHK2A

The event.latitude and event.longitude come through correctly but the altitude is coming out as the default value, 0.  I've tested this on an iPhone 3GS and iPad 2 with the same result in each case.  Code below.

package
{
    import flash.display.MovieClip;
    import flash.sensors.Geolocation;
    import flash.events.GeolocationEvent;
    import flash.events.StatusEvent;
    import flash.events.MouseEvent;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.errors.IOError;
    import flash.display.Loader;
  
    public class GeoTest extends MovieClip {
       
        private var geo:Geolocation;
        private var loader:Loader;
       
        public function GeoTest() {
           if (Geolocation.isSupported)
            {
                geo = new Geolocation();

                geo.addEventListener(StatusEvent.STATUS, geolocationStatus);
            }
            else
            {
                geoResults.text = "Sorry, geolocation isn't working on this device. But don't feel bad, here's a cool map to look at.";
                loadMap();
            }
            getGeo.addEventListener(MouseEvent.CLICK, handleButton);
        }

       private function handleButton(event:MouseEvent):void
        {
            geoStatus.text = "click";
            if (geo)
            {
                geoStatus.text = "Attempting to connect...";
                geo.setRequestedUpdateInterval(500);
                geo.addEventListener(GeolocationEvent.UPDATE, updateHandler);
                // If this isn't working on the device, be sure to set permissions in the AIR Android Settings dialog
            }
        }

        private function geolocationStatus( e :StatusEvent ):void
        {
            if (e.code == "Geolocation.Unmuted")
            {
                geoStatus.text = "GPS is turned on \n";
            }
            else
            {
                geoStatus.text = "GPS is turned off \n";
            }
        }

        private function updateHandler(event:GeolocationEvent):void
        {
            geoResults.text = "latitude: " + event.latitude.toString() + "\n"
                        + "longitude: " + event.longitude.toString() + "\n"
                        + "altitude: " + event.altitude.toString()  + "\n"
                        + "speed: " + event.speed.toString()  + "\n"
                        + "heading: " + event.heading.toString()  + "\n"
                        + "horizontal accuracy: " + event.horizontalAccuracy.toString()  + "\n"
                        + "vertical accuracy: " + event.verticalAccuracy.toString() ;
           
            loadMap(event.latitude.toString(), event.longitude.toString());
           
            geo.removeEventListener(GeolocationEvent.UPDATE, updateHandler);
        }

        private function loadMap(lat:String=null, long:String=null):void
        {
            geoStatus.text = "Requesting map...";
            var locString:String;
            var markers: String;
           
            if(lat != null)
            {
                markers = "&markers=color:blue|label:M|" + lat + ", " + long;
                locString = "http://maps.google.com/maps/api/staticmap?center=" + lat + ", " + long + "&zoom=14&size=300x200&" + markers + "&sensor=true";
            } else {
                // Adobe MAX Conference in LA.
                markers = "&markers=color:red|label:A|34.040463, -118.268681";
                locString = "http://maps.google.com/maps/api/staticmap?center=34.040463, -118.268681&zoom=15&size=300x200&maptype=hybrid&" + markers + "&sensor=false";
            }
           
            loader = new Loader();
           
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleMapResponse);
            loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, handleMapError);
            loader.load(new URLRequest(locString));
        }
       
        private function handleMapResponse(event:Event):void
        {
            loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, handleMapResponse);
            loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, handleMapError);
           
            geoStatus.text = "Map retrieved successfully";
           
            mapHolder.addChild(loader);
        }
       
        private function handleMapError(event:IOError):void
        {
            loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, handleMapResponse);
            loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, handleMapError);
           
            geoStatus.text = "Unable to retrieve map from Google Static API";
        }
    }
}

This topic has been closed for replies.

1 reply

June 27, 2011

Hi,

Have you tried in a full GPS enabled area ? Fix obtained inside a building from Wi-Fi or cell network may not be that accurate.

Thanks,

Meet