Geolocation in Flash

后端 未结 1 619
日久生厌
日久生厌 2021-01-20 01:46

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

相关标签:
1条回答
  • 2021-01-20 02:33

    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);
    
        }
    
    0 讨论(0)
提交回复
热议问题