Motion & Location

Reference ❯ Detecting Device Motion and Physical Location

Detecting Motion

Gravity top ↑

Syntax

Gravity
Gravity.x
Gravity.y
Gravity.z

This global variable represents the current direction and amount of gravity relative to the screen orientation while the viewer is running. The y component of this vector will almost always be negative, indicating that the device is being held upright.

x

float, the amount of gravity in the x direction

y

float, the amount of gravity in the y direction

z

float, the amount of gravity in the z direction

UserAcceleration top ↑

Syntax

UserAcceleration
UserAcceleration.x
UserAcceleration.y
UserAcceleration.z    

This global variable represents the current acceleration relative to the device while the viewer is running. You can use this to detect things such as shaking.

x

float, the amount of acceleration in the x direction

y

float, the amount of acceleration in the y direction

z

float, the amount of acceleration in the z direction

RotationRate top ↑

Syntax

RotationRate
RotationRate.x
RotationRate.y
RotationRate.z    

This global variable represents the rotation rate of the device around three axes. Internally, this makes use of the device gyroscope (if available). On devices where a gyroscope is not available, RotationRate is a zero vector.

x

float, the amount of rotation around the x axis

y

float, the amount of rotation around the y axis

z

float, the amount of rotation around the z axis

Location

location.enable() top ↑

Syntax

location.enable()

Calling this function requests that the device should begin monitoring its physical location. You must call this before you can reliably read location data. Upon first activation the device will request permission to access its location, permission must be granted in order to use location functions. Location updates are disabled by default.

Examples

function setup() -- enable location updating location.enable() print(location) end

location.disable() top ↑

Syntax

location.disable()

Calling this function requests that the device should stop monitoring its physical location. Disabling location updates preserves battery power.

location.available() top ↑

Syntax

location.available()

This function returns whether location data is available. If location updating has not been enabled, or is not permitted, this function will return false.

Returns

A boolean value specifying whether location data is available

location.distanceTo(lat, lon) top ↑

Syntax

location.distanceTo(lat, lon)

This function returns the distance in meters from the current location (if available) to the specified latitude and longitude.

Returns

Distance in meters between the current location and specified coordinates

location.distanceBetween(lat1,lon1,lat2,lon2) top ↑

Syntax

location.distanceBetween(lat1, lon1, lat2, lon2)

This function returns the distance in meters between two locations specified by the coordinates lat1, lon1, lat2, lon2.

Returns

Distance in meters between the two specified coordinates

location.latitude top ↑

Syntax

location.latitude

This value specifies the current latitude if location is available.

Returns

Current latitude, if available. Nil if not.

location.longitude top ↑

Syntax

location.longitude

This value specifies the current longitude if location is available.

Returns

Current longitude, if available. Nil if not.

location.altitude top ↑

Syntax

location.altitude

This value specifies the current altitude if location is available. Positive values indicate altitudes above sea level. Negative values indicate altitudes below sea level.

Returns

Current altitude, if available. Nil if not.

location.horizontalAccuracy top ↑

Syntax

location.horizontalAccuracy

This value specifies the horizontal accuracy of the latitude and longitude of the current location. This value indicates the radius of the circle in which the latitude and longitude are at the center.

A negative value indicates that the location’s latitude and longitude are invalid.

Returns

Current horizontal accuracy, if available. Nil if not.

location.verticalAccuracy top ↑

Syntax

location.verticalAccuracy

This value specifies the accuracy of the altitude of the current location. The altitude value could be plus or minus this value.

A negative value indicates that the current altitude is invalid.

Returns

Current vertical accuracy, if available. Nil if not.

location.speed top ↑

Syntax

location.speed

This value specifies the current speed of the device in meters per second. A negative value indicates an invalid speed.

Returns

Current speed, if available. Nil if not.

location.course top ↑

Syntax

location.course

This variable specifies the current heading of the device measured in degrees starting at due north and moving clockwise around the compass. A negative value indicates an invalid direction.

Returns

Current course, if available. Nil if not.