3

I'm building a small web app in Flash. Is there a solution to get the geo-location of a user?

Uli
  • 2,625
  • 10
  • 46
  • 71

1 Answers1

3

The easiest way is to interface with a JavaScript function.

In your HTML:

    <script>
        function getGEO()
        {
            // First check if your browser supports the geolocation API
            if (navigator.geolocation)
            {
                //alert("HTML 5 is getting your location");
                // Get the current position
                navigator.geolocation.getCurrentPosition(function(position)
                {
                    lat = position.coords.latitude
                    long = position.coords.longitude;
                    // Pass the coordinates to Flash
                    passGEOToSWF(lat, long);
                });
            } else {
                //alert("Sorry... your browser does not support the HTML5 GeoLocation API");
            }
        }
        function passGEOToSWF(lat,long)
        {
            //alert("HTML 5 is sending your location to Flash");
            // Pass the coordinates to mySWF using ExternalInterface
            document.getElementById("index").passGEOToSWF(lat,long);
        }
    </script>

Then, in your Application, once your map is ready, put this in a function:

     //for getting a user's location
            if (ExternalInterface.available) 
            {
                //check if external interface is available
                try 
                {
                    // add Callback for the passGEOToSWF Javascript function
                    ExternalInterface.addCallback("passGEOToSWF", onPassGEOToSWF);
                } 
                catch (error:SecurityError) 
                {
                    // Alert the user of a SecurityError
                } 
                catch (error:Error) 
                {
                    // Alert the user of an Error
                }
            }

Finally, have a private function ready to catch the callback.

    private function onPassGEOToSWF(lat:*,long:*):void
    {
        userLoc = new LatLng(lat,long);
        map.setCenter(userLoc);

    }
the_skua
  • 1,230
  • 17
  • 30
  • Awesome! Thank you. Do you have an idea how to get the geolocation from an Adobe Air desktop app also? – Uli Jan 21 '12 at 18:10
  • Damn, I thought there would be a workaround available. Thanks though. – Uli Jan 23 '12 at 03:03
  • I meant to say that *I* have never worked with Air, so I don't know how to do it. Sorry about the poor wording. – the_skua Jan 23 '12 at 15:52