我正在Flash中构建一个小型Web应用程序。是否有解决方案来获取用户的地理位置?

最佳答案

最简单的方法是与JavaScript函数交互。

在您的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>


然后,在您的应用程序中,准备好地图后,将其放入函数中:

     //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
                }
            }


最后,准备好一个私有函数来捕获回调。

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

    }

08-15 16:50