地理位置在Flash地理位置、Flash

2023-09-08 12:50:55 作者:竹外桃花依旧

我建立在Flash中的小Web应用程序。是否有解决方案,以获取用户的地理位置?

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

推荐答案

最简单的方法是用JavaScript函数的接口。

The easiest way is to interface with a JavaScript function.

在你的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);

    }
 
精彩推荐
图片推荐