检测 geolocation 更改

要使用 geolocation 传感器,请实例化 Geolocation 对象并注册该对象调度的 update 事件。 update 事件是一个 Geolocation 事件对象。此事件包含下列八个属性:

  • altitude — 高度(以米为单位)。

  • heading — 相对于正北的移动方向(以度为单位)。

  • horizontalAccuracy — 水平精度(以米为单位)。

  • latitude — 纬度(以度为单位)。

  • longitude — 经度(以度为单位)。

  • speed — 速度(以米/秒为单位)。

  • timestamp — 自初始化运行时后事件的毫秒数。

  • verticalAccuracy — 垂直精度(以米为单位)。

timestamp 属性是一个 int 对象。其他属性是 Number 对象。

以下是在文本字段中显示 geolocation 数据的一个基本示例:

var geo:Geolocation; 
if (Geolocation.isSupported) 
{ 
    geo = new Geolocation(); 
    geo.addEventListener(GeolocationEvent.UPDATE, updateHandler); 
} 
else 
{ 
    geoTextField.text = "Geolocation feature not supported"; 
} 
function updateHandler(event:GeolocationEvent):void 
{ 
    geoTextField.text = "latitude: " + event.latitude.toString() + "\n" 
            + "longitude: " + event.longitude.toString() + "\n" 
            + "altitude: " + event.altitude.toString() 
            + "speed: " + event.speed.toString() 
            + "heading: " + event.heading.toString() 
            + "horizontal accuracy: " + event.horizontalAccuracy.toString() 
            + "vertical accuracy: " + event.verticalAccuracy.toString() 
}

要使用此示例,请确保创建 geoTextField 文本字段并在使用此代码之前将该文本字段添加到显示列表。

您可以通过调用 Geolocation 对象的 setRequestedUpdateInterval() 方法调整 geolocation 事件的所需时间间隔。此方法使用一个参数 interval ,该参数是请求的更新时间间隔(以毫秒为单位):

var geo:Geolocation = new Geolocation(); 
geo.setRequestedUpdateInterval(10000);

Geolocation 更新之间的实际时间可能大于或小于此值。更新间隔的任何更改都会影响所有注册侦听器。如果您不调用 setRequestedUpdateInterval() 方法,应用程序将根据设备的默认时间间隔接收更新。

用户可以阻止应用程序访问 geolocation 数据。例如,应用程序尝试获取 geolocation 数据时,iPhone 会提示用户。为响应该提示,用户可以拒绝应用程序访问 geolocation 数据。当用户不能访问 geolocation 数据时,Geolocation 对象将调度 status 事件。另外,Geolocation 对象还包含 muted 属性,当 geolocation 传感器不可用时,该属性设置为 true 。当 muted 属性发生更改时,Geolocation 对象将调度 status 事件。以下代码显示如何检测 geolocation 数据在哪些情况下不可用:

package 
{ 
    import flash.display.Sprite; 
    import flash.display.StageAlign; 
    import flash.display.StageScaleMode; 
    import flash.events.GeolocationEvent; 
    import flash.events.MouseEvent; 
    import flash.events.StatusEvent; 
    import flash.sensors.Geolocation; 
    import flash.text.TextField; 
    import flash.text.TextFormat; 
 
    public class GeolocationTest extends Sprite 
    { 
         
        private var geo:Geolocation; 
        private var log:TextField; 
         
        public function GeolocationTest() 
        {             
            super(); 
            stage.align = StageAlign.TOP_LEFT; 
            stage.scaleMode = StageScaleMode.NO_SCALE; 
            setUpTextField(); 
             
            if (Geolocation.isSupported) 
            { 
                geo = new Geolocation(); 
                if (!geo.muted) 
                { 
                    geo.addEventListener(GeolocationEvent.UPDATE, geoUpdateHandler); 
                } 
                geo.addEventListener(StatusEvent.STATUS, geoStatusHandler); 
            } 
            else 
            { 
                log.text = "Geolocation not supported"; 
            } 
        } 
         
        public function geoUpdateHandler(event:GeolocationEvent):void 
        { 
            log.text = "latitude : " + event.latitude.toString() + "\n"; 
            log.appendText("longitude : " + event.longitude.toString() + "\n"); 
        } 
         
        public function geoStatusHandler(event:StatusEvent):void 
        { 
            if (geo.muted) 
                geo.removeEventListener(GeolocationEvent.UPDATE, geoUpdateHandler); 
            else 
                geo.addEventListener(GeolocationEvent.UPDATE, geoStatusHandler); 
        } 
         
        private function setUpTextField():void 
        { 
            log = new TextField(); 
            var format:TextFormat = new TextFormat("_sans", 24); 
            log.defaultTextFormat = format; 
            log.border = true; 
            log.wordWrap = true; 
            log.multiline = true; 
            log.x = 10; 
            log.y = 10; 
            log.height = stage.stageHeight - 20; 
            log.width = stage.stageWidth - 20; 
            log.addEventListener(MouseEvent.CLICK, clearLog); 
            addChild(log); 
        } 
        private function clearLog(event:MouseEvent):void 
        { 
            log.text = ""; 
        } 
    } 
}
注: 不包括 GPS 单位的第一代 iPhone 仅偶尔调度 update 事件。在这些设备上,Geolocation 对象最初调度一个或两个 update 事件。随后,当信息明显更改时将调度 update 事件。

检查 geolocation 支持

使用 Geolocation.isSupported 属性测试运行时环境是否能够使用此功能:

if (Geolocation.isSupported) 
{ 
    // Set up geolocation event listeners and code. 
}

目前,仅 iPhone 的基于 ActionScript 的应用程序和 Flash Lite 4 支持此 geolocation。如果 Geolocation.isSupported 在运行时为 true ,则存在 geolocation 支持。

某些 iPhone 模型没有 GPS 单位。这些模型使用其他方法(如移动电话三定位)获取 geolocation 数据。对于这些模型或任何已禁用 GPS 的 iPhone,Geolocation 对象可能只调度一个或两个初始 update 事件。