The location sensor can be used to access the location of the device. The sensor combines the WiFi, cellular, and GPS data from the device to determine the location. This data can be accessed using the location tag.
The location data is stored inside of an inherent vector called value inside of the onValueChanged event. value is an object with the following fields:
- Latitude - measured in degrees
- Longitude - measured in degrees
- Altitude - measured in metres above sea level
- Speed - measured in metres per second
- Bearing - measured in degrees clockwise from true North
- Timestamp - object with fields:
The location sensor also has a field to set the accuracy of the data. The default accuracy balances power consumption with data accuracy. The accuracy settings are as follows:
- high - accuracy within approximately 3 m
- good - accuracy within approximately 10 m
- balanced - accuracy within approximately 100 metres
- low - accuracy within approximately 1 km
- lowest - accuracy within approximately 3 km
As with simulations, qdex will not start to read location data until the sensor is started using the Start() method, or signaled from a simulation.
The location sensor may take upwards of 1-2 minutes to read data.
This example uses a simulation to signal the location sensor on a device. The duration of the simulation is defined so that the location sensor can time out, thus reducing battery power consumption.
The simulation reads the location data every 8 seconds to check for updates. Once the data changes from the default value of 0, the labels are updated to reflect the sensor numbers. The notify popup is used to let users know that the sensor is on, but needs time to calculate the location data.
<location accuracy="best" name="myLocation" /> <button name="startButton" content="Get Location Data"> <onClick> mySim:Start() notify('Starting Location Sensor') startButton.Style.Visibility = "hidden" </onClick> </button> <simulation name="mySim" period="8" duration="180"> <onDuration> startButton.Style.Visibility = "visible" </onDuration> <solver> <series> <signal ref="section1.myLocation" width="5" /> <system name="myOutput"> <input name="location" width="5" /> <onOutputs> if myLocation.Latitude == 0 and myLocation.Longitude == 0 and myLocation.Altitude == 0 then notify('Calculating...') else latitudeLabel.Text = string.format("Latitude: %.2f degrees", myLocation.Latitude) longitudeLabel.Text = string.format("Longitude: %.2f degrees", myLocation.Longitude) altitudeLabel.Text = string.format("Altitude: %.2f metres above sea level", myLocation.Altitude) speedLabel.Text = string.format("Speed: %.2f m/s", myLocation.Speed) bearingLabel.Text = string.format("Bearing: %.2f degrees CW from North", myLocation.Bearing) mySim:Stop() startButton.Style.Visibility = "visible" end </onOutputs> </system> </series> </solver> </simulation> <label name="latitudeLabel" /> <label name="longitudeLabel" /> <label name="altitudeLabel" /> <label name="speedLabel" /> <label name="bearingLabel" /> <label name="timestampLabel" />