Packageflash.sensors
Classpublic class Geolocation
InheritanceGeolocation Inheritance EventDispatcher Inheritance Object

Language version: ActionScript 3.0
Runtime version: AIR 2

The Geolocation class dispatches events in response to the device's location sensor.

If a device supports geolocation, you can use this class to obtain the current geographical location of the device. The geographical location is displayed on the device in the form of latitudinal and longitudinal coordinates (in WGS-84 standard format). When the location of the device changes, you can receive updates about the changes. If the device supports this feature, you will be able to obtain information about the altitude, accuracy, heading, speed, and timestamp of the latest change in the location.

AIR profile support: This feature is supported only on mobile devices. It is not supported on desktop or AIR for TV devices. You can test for support at run time using the Geolocation.isSupported property. See AIR Profile Support for more information regarding API support across multiple profiles.

Note: To enable Geolocation in iOS, ensure that you add a key-value pair to the infoAdditions element in the application xml file.See iOS Settings for more information on the infoAdditions element.

View the examples.

See also

Cristophe Coenraets: Mulit-user Google Maps Collaboration


Public Properties
 PropertyDefined by
 Inheritedconstructor : Object
A reference to the class object or constructor function for a given object instance.
Object
  desiredAccuracy : String
This property determines the accuracy of the geolocation data on iOS.
Geolocation
  isSupported : Boolean
[static][read-only] Whether a location sensor is available on the device (true); otherwise false.
Geolocation
  locationAlwaysUsePermission : Boolean
This property determines the access permission type usage of geolocation.
Geolocation
  muted : Boolean
[read-only] Specifies whether the user has denied access to the geolocation (true) or allowed access (false).
Geolocation
  pausesLocationUpdatesAutomatically : Boolean
This property determines whether the geolocation services should pause due to app inactivity when application goes into background (on iOS).
Geolocation
  permissionStatus : String
[static][read-only] Determine whether the application has been granted the permission to use geolocation services.
Geolocation
 Inheritedprototype : Object
[static] A reference to the prototype object of a class or function object.
Object
Public Methods
 MethodDefined by
  
Creates a new Geolocation instance.
Geolocation
 Inherited
addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
Registers an event listener object with an EventDispatcher object so that the listener receives notification of an event.
EventDispatcher
 Inherited
Dispatches an event into the event flow.
EventDispatcher
 Inherited
Checks whether the EventDispatcher object has any listeners registered for a specific type of event.
EventDispatcher
 Inherited
Indicates whether an object has a specified property defined.
Object
 Inherited
Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter.
Object
 Inherited
Indicates whether the specified property exists and is enumerable.
Object
 Inherited
removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
Removes a listener from the EventDispatcher object.
EventDispatcher
  
Requests geolocation permission for the application.
Geolocation
 Inherited
Sets the availability of a dynamic property for loop operations.
Object
  
Used to set the time interval for updates, in milliseconds.
Geolocation
 Inherited
Returns the string representation of this object, formatted according to locale-specific conventions.
Object
 Inherited
Returns the string representation of the specified object.
Object
 Inherited
Returns the primitive value of the specified object.
Object
 Inherited
Checks whether an event listener is registered with this EventDispatcher object or any of its ancestors for the specified event type.
EventDispatcher
Events
 EventSummaryDefined by
 Inherited [broadcast event] Dispatched when the Flash Player or AIR application gains operating system focus and becomes active.EventDispatcher
 Inherited [broadcast event] Dispatched when the Flash Player or AIR application operating loses system focus and is becoming inactive.EventDispatcher
   The Geolocation object dispatches status events when the user changes access to the location sensor.Geolocation
   The update event is dispatched in response to updates from the location sensor.Geolocation
Public Constants
 ConstantDefined by
  LOCATION_ACCURACY_BEST : String = "locationAccuracyBest"
[static]

The best level of accuracy available.

Geolocation
  LOCATION_ACCURACY_BEST_FOR_NAVIGATION : String = "locationAccuracyBestForNavigation"
[static]

The highest possible accuracy that uses additional sensor data to facilitate navigation apps.

Geolocation
  LOCATION_ACCURACY_HUNDRED_METERS : String = "locationAccuracyHundredMeters"
[static]

Accurate to within one hundred meters.

Geolocation
  LOCATION_ACCURACY_KILOMETER : String = "locationAccuracyKilometer"
[static]

Accurate to the nearest kilometer.

Geolocation
  LOCATION_ACCURACY_NEAREST_TEN_METERS : String = "locationAccuracyNearestTenMeters"
[static]

Accurate to within ten meters of the desired target.

Geolocation
  LOCATION_ACCURACY_THREE_KILOMETERS : String = "locationAccuracyThreeKilometers"
[static]

Accurate to the nearest three kilometers.

Geolocation
Property detail
desiredAccuracyproperty
desiredAccuracy:String  [read-write]

Language version: ActionScript 3.0
Runtime version: AIR 32

This property determines the accuracy of the geolocation data on iOS. Setting pausesLocationUpdatesAutomatically to false can cause battery drain as it keeps fetching geolocation data. Accuracy can be set to LOCATION_ACCURACY_BEST_FOR_NAVIGATION, LOCATION_ACCURACY_BEST, LOCATION_ACCURACY_NEAREST_TEN_METERS, LOCATION_ACCURACY_HUNDRED_METERS, LOCATION_ACCURACY_KILOMETER or LOCATION_ACCURACY_THREE_KILOMETERS based on the app's usage scenario. Set the property after calling requestPermission() and enabling location services in background. The default value of the property is LOCATION_ACCURACY_BEST which is the best level of accuracy available.

Implementation
    public function get desiredAccuracy():String
    public function set desiredAccuracy(value:String):void
isSupportedproperty 
isSupported:Boolean  [read-only]

Language version: ActionScript 3.0
Runtime version: AIR 2

Whether a location sensor is available on the device (true); otherwise false.

Implementation
    public static function get isSupported():Boolean
locationAlwaysUsePermissionproperty 
locationAlwaysUsePermission:Boolean  [read-write]

Language version: ActionScript 3.0
Runtime version: AIR 29

This property determines the access permission type usage of geolocation. The permission type can be Always or When In Use. If the property is set to false, it requests When In Use permission else it requests Always use permission. Set the property before calling requestPermission(). The default value of the property is false.

Implementation
    public function get locationAlwaysUsePermission():Boolean
    public function set locationAlwaysUsePermission(value:Boolean):void
mutedproperty 
muted:Boolean  [read-only]

Language version: ActionScript 3.0
Runtime version: AIR 2

Specifies whether the user has denied access to the geolocation (true) or allowed access (false). When this value changes, a status event is dispatched.

Implementation
    public function get muted():Boolean

See also

pausesLocationUpdatesAutomaticallyproperty 
pausesLocationUpdatesAutomatically:Boolean  [read-write]

Language version: ActionScript 3.0
Runtime version: AIR 32

This property determines whether the geolocation services should pause due to app inactivity when application goes into background (on iOS). The value can be true or false. If the property is set to false, it ensures continous updates of geolocation services. Set the property after calling requestPermission() and enabling location services in background. The default value of the property is true.

Implementation
    public function get pausesLocationUpdatesAutomatically():Boolean
    public function set pausesLocationUpdatesAutomatically(value:Boolean):void
permissionStatusproperty 
permissionStatus:String  [read-only]

Language version: ActionScript 3.0
Runtime version: AIR 24.0

Determine whether the application has been granted the permission to use geolocation services.

Implementation
    public static function get permissionStatus():String

See also

Constructor detail
Geolocation()constructor
public function Geolocation()

Language version: ActionScript 3.0
Runtime version: AIR 2

Creates a new Geolocation instance.

Method detail
requestPermission()method
public function requestPermission():void

Language version: ActionScript 3.0
Runtime version: AIR 24.0

Requests geolocation permission for the application.


Events
PermissionStatus:PermissionEvent — dispatched when the requested permission is granted/denied by the user.
setRequestedUpdateInterval()method 
public function setRequestedUpdateInterval(interval:Number):void

Language version: ActionScript 3.0
Runtime version: AIR 2

Used to set the time interval for updates, in milliseconds. The update interval is only used as a hint to conserve the battery power. The actual time between location updates may be greater or lesser than this value.Any change in the update interval using this method affects all registered update event listeners. The Geolocation class can be used without calling the setRequestedUpdateInterval method. In this case, the platform will return updates based on its default interval.

Note: First-generation iPhones, which do not include a GPS unit, dispatch update events only occasionally. On these devices, a Geolocation object initially dispatches one or two update events. It then dispatches update events when information changes noticeably.

Parameters
interval:Number — requested update interval. If interval <= 0, then any call to this method has no effect.

Throws
ArgumentError — The specified interval is less than zero.
Event detail
statusevent  
Event object type: flash.events.StatusEvent
StatusEvent.type property = flash.events.StatusEvent.STATUS

Language version: ActionScript 3.0
Runtime version: AIR 2

The Geolocation object dispatches status events when the user changes access to the location sensor. For example, if, in response to a device prompt, the user prevents the application from accessing location data, the Geolcation object dispatches a status event. When the status changes to a state where the location sensor is not available, the muted property of the Geolocation instance is true.

Defines the value of the type property of a status event object.

This event has the following properties:

PropertyValue
bubblesfalse
cancelablefalse; there is no default behavior to cancel.
codeA description of the object's status.
currentTargetThe object that is actively processing the Event object with an event listener.
levelThe category of the message, such as "status", "warning" or "error".
targetThe object reporting its status.

See also

updateevent  
Event object type: flash.events.GeolocationEvent
GeolocationEvent.type property = flash.events.GeolocationEvent.UPDATE

Language version: ActionScript 3.0
Runtime version: AIR 2

The update event is dispatched in response to updates from the location sensor. The event is dispatched under the following circumstances:

Note: First-generation iPhones, which do not include a GPS unit, dispatch update events only occasionally. On these devices, a Geolocation object initially dispatches one or two update events. It then dispatches update events when information changes noticeably.

Defines the value of the type property of a GeolocationEvent event object.

This event has the following properties:

PropertyValue
bubblesfalse
cancelablefalse; there is no default behavior to cancel.
currentTargetThe object that is actively processing the event object with an event listener.
targetThe Geolocation object receiving data.
Constant detail
LOCATION_ACCURACY_BESTconstant
public static const LOCATION_ACCURACY_BEST:String = "locationAccuracyBest"

Language version: ActionScript 3.0
Runtime version: AIR 32

The best level of accuracy available.

LOCATION_ACCURACY_BEST_FOR_NAVIGATIONconstant 
public static const LOCATION_ACCURACY_BEST_FOR_NAVIGATION:String = "locationAccuracyBestForNavigation"

Language version: ActionScript 3.0
Runtime version: AIR 32

The highest possible accuracy that uses additional sensor data to facilitate navigation apps.

LOCATION_ACCURACY_HUNDRED_METERSconstant 
public static const LOCATION_ACCURACY_HUNDRED_METERS:String = "locationAccuracyHundredMeters"

Language version: ActionScript 3.0
Runtime version: AIR 32

Accurate to within one hundred meters.

LOCATION_ACCURACY_KILOMETERconstant 
public static const LOCATION_ACCURACY_KILOMETER:String = "locationAccuracyKilometer"

Language version: ActionScript 3.0
Runtime version: AIR 32

Accurate to the nearest kilometer.

LOCATION_ACCURACY_NEAREST_TEN_METERSconstant 
public static const LOCATION_ACCURACY_NEAREST_TEN_METERS:String = "locationAccuracyNearestTenMeters"

Language version: ActionScript 3.0
Runtime version: AIR 32

Accurate to within ten meters of the desired target.

LOCATION_ACCURACY_THREE_KILOMETERSconstant 
public static const LOCATION_ACCURACY_THREE_KILOMETERS:String = "locationAccuracyThreeKilometers"

Language version: ActionScript 3.0
Runtime version: AIR 32

Accurate to the nearest three kilometers.

Examples
examples\GeolocationExample
In the following example, the application displays the latitude, longitude, and horizontal accuracy of geolocation update events as they are received.
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, geoUpdateHandler);
		}
		
		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 = "";
		}
	}
}