The LocationSensor component is a simple control that is difficult to use without knowledge of some basic concepts of geo-location. The LocationSensor is used to communicate with the global positioning satellite receiver (GPS) in your phone/tablet. When the LocationSensor communicates with the built-in GPS receiver, the GPS determines the location of your device. The sensor can also work with network/wifi location services. Finding a location through the network uses very different techniques than with a GPS. Location means the device's present latitude and longitude, or it can mean your street address. The measuring units employed in the LocationSensor for distance are meters. Time is measured in milliseconds (ms). Be aware that one second = 1000 ms. and 60000 ms is one minute.
When the sensor reports distance information or you set a distance into the component, the units are in meters. If your app must deal in English units, use the Math blocks to convert units at the time you display them. Calculate everything in meters, then convert to report the result in feet or miles on your display. Think meters!
A nautical mile is the distance subtended by a minute of angle (1/60 of a degree) across the earth's radius. This means that the distance between a degree of latitude and the next whole degree is sixty nautical miles. A degree of longitude is sixty nautical miles at the equator, but the separation between adjacent whole degrees of longitude diminishes as you change latitude toward the poles. The spacings between degrees of latitude are constant; the spacing between degrees of longitude are variable.
Current app developers think in terms of meters, but a long time ago, navigators described their ship's position with respect to the English units of degrees, minutes and seconds. Latitude and longitude are still described in terms of degrees, minutes and seconds. These are awkward units for use on a computer, so developers and others commonly use decimal latitude and longitude descriptions, to make the math calculations easier. Looking at 0.00001 of a degree of latitude, it converts to 0.9144 meters. Therefore, satellite positional information is precise to 0.9 meters. The best accuracy possible with specialized GPS receivers is approximately three meters. There are techniques to make the resolution better. As you develop apps with this tool, be aware the GPS in your device can do a lot, but its accuracy is limited in many situations.
You will do your best job of developing apps using the LocationSensor if you understand how a GPS system works. Geo-location principals are described near the end of the tutorial.
GPS Accuracy Logger is an app that demonstrates the use of a device's Global Positioning Satellite (GPS) receiver. The app also demonstrates how the accuracy of a device's GPS varies depending on where the device is located. The LocationSensor component instructs the GPS receiver in the mobile device to get a satellite fix (find a satellite, determine that the information from the satellite is valid, find at least two more satellites to confirm the information, and only then report the information back to your device). Some GPSes can use from between 12 and 20 satellites to get a very accurate fix, providing the GPS receiver is capable of receiving 12 or 20 channels of data. Many simple devices will only use a few satellites.
The logger app captures latitude/longitude positional data and provides a numerical estimate of the reliability of the GPS' satellite fix (Accuracy). Small accuracy values indicate better accuracy. The app reports what the GPS resolves as latitude and longitude and demonstrates how the device accuracy might vary as the GPS updates its satellite fixes, or as the device is moved from outside to inside a building or loses a fix on the satellites it uses to determine location.
Note in the image below the change in the accuracy readings captured over a short period of time (eight minutes). This device was stationary on a desk inside a building when it captured the readings and there is significant variation in the ability of the device to provide accurate location information. Satellites continually move around the Earth. The GPS in the device loses accuracy as the number of satellites it can receive decreases. The accuracy normally increases as the GPS receives more satellites. MIT App Inventor can report the accuracy of any fix provided the GPS receiver on the device has the capability of reporting accuracy (one of the many properties of the LocationSensor described later). The GPS is designed to use as much information and from as many satellites as possible to provide the best possible accuracy at any instant in time. Below is a discussion of why accuracy matters, what causes it to fluctuate and what a developer can do to minimize the impact on the users of the app by avoiding the reporting of 'suspicious' location data.
Note: To test this app, you must use a device with GPS capabilities. Testing with an emulator will not give you true location information. Below is a short discussion of what can be done to provide location information by connecting to a network or through WIFI. Phones/tablets without a GPS receiver can provide location data (although it is less precise).
Satellites transmit information using NMEA 0183 format, a standard for communicating with marine electronic devices, also used for GPSes. The NMEA data stream is a compilation of lots of data transmitted from a satellite in text format. MIT App Inventor cannot interpret all the information available in the data stream broadcast from the satellites. Despite the limitations of the LocationSensor component, AI2 provides the basic functionality needed to get location information from your device.
The GPS Accuracy Logger app requires just a few components and blocks. In the Designer, add the following components, and set the properties as listed.
|Component||Component Drawer||Component Name||Purpose||Properties|
|Label||User Interface||LatitudeLabel||Displays the current latitude reading from the LocationSensor||Text: Latitude|
|Label||User Interface||LongitudeLabel||Displays the current longitude reading from the LocationSensor||Text: Longitude|
|HorizontalArrangement||Layout||HorizontalArrangement1||Holds the next 3 Labels so they appear next to each other||Width: Fill Parent|
|Label||User Interface||AccuracyNowLabel||Displays label for presenting current accuracy||Text: Accuracy Now:|
|Label||User Interface||AccuracyLabel||Displays the current accuracy value from the LocationSensor||Text: accuracy|
|Label||User Interface||MetersLabel||Displays the label for +/- meters post-accuracy label||Text: +/- meters|
|Label||User Interface||WarningLabel||Notifies user of information about GPS accuracy on devices||Width: 98%
Text: *** Make sure GPS is enabled on your device. Not all devices can display GPS accuracy. ***
|Label||User Interface||DataHeaderLabel||Display heading for accuracy data||
Text:" Time +/-M Latitude Longitude"
|Label||User Interface||DataLabel||Display data received from LocationSensor||Text: "" (blank)|
|LocationSensor||Sensors||LocationSensor1||Provides location information from GPS within device||
|Clock||Sensors||Clock1||Will be used to poll the LocationSensor periodically for accuracy information.||TimerInterval1000|
Your Designer Viewer and Component list should look similar to this:
Switch to the Blocks Editor. Initialize a variable LS_Accuracy to 1. This will hold the accuracy value we receive from the LocationSensor.
The LocationSensor1.LocationChanged event block is triggered when the GPS confirms a location change on the device. The latitude and longitude reported are in decimal degrees. Set the reported latitude and longitude in the appropriate labels in the app.
The Clock1.Timer will trigger every second and check the accuracy of the LocationSensor. The if-else block tests to see if the accuracy is less than or equal to 10 meters and changes the text color for the AccuracyLabel to either black (<= 10 meters) or red (> 10 meters).
The second if-then block compares the current accuracy value with the accuracy determined during a previous check and stored in the LS_Accuracy variable. If the accuracy has changed, the app reports a new line in the data list. The list enumerates the latest accuracy, latitude and longitude. This “list” is displayed at the bottom of the app's screen. The \n symbol is used to force each data set (consisting of time, accuracy, latitude, longitude) onto individual lines.
And finally, the accuracy is saved to the variable LS_Accuracy for comparison on the next iteration.
What is the \n in the Text blocks for? This symbol (consisting of a backslash and the character n) forces a line change in a Label. Placing the symbol in a text block helps to make the formatting readable when the DataLabel is updated on the screen.
When does the location sensor 'know' when to check for a satellite fix? There are two properties that can trigger this. First is the TimeInterval property. In this app, we've set the TimeInterval to 60000, or 1 minute. The LocationSensor will then check each minute for new location information. The DistanceInterval property is another way to trigger new data. In this app, the property is set to 0, so any location change detected (after 1 minute) will trigger the LocationSensor1.LocationChanged event.
Try out the app with the MIT AI2 Companion. You should start to receive data and the accuracy value should change. If you do not, check that your MIT AI Companion app has location permissions turned on in its settings. You should be prompted, but you can also check the app's settings.
If you build the apk file and install this app on your device, you should also be prompted during the installation to turn on location permissions for the app. If not, make sure to go into the Settings for the app and turn on location permissions before using the app.
This app is much more complex and demonstrates most of the capabilities of the AI2 LocationSensor component. Here is a link to the aia so you can explore the code. The two images below show a working Location Sensor Test app. The image to the left shows the sensor test app in action but prior to touching the Capabilities button. Touch the button and the screen should look like the image on the right. The app confirms the tablet's GPS has all the required capabilities.
The app features the following:
Understanding how GPS and Wireless Networks work is necessary to understand the LocationSensor component. Both GPS and Wireless networks provide positional information in different ways. The LocationSensor component can obtain information using either system.
Anything you want to know about GPS and how GPS receivers works is summarized here: http://en.wikipedia.org/wiki/Global_Positioning_System. Wikipedia is not always the most accurate source, but in this instance Wikipedia provides a pretty good introduction to geolocation concepts.
The GPS satellites broadcast two types of data called Almanac and Ephemeris. Almanac data includes the satellite orbital parameters. It is precise data and is valid for several months. Ephemeris data includes orbital and clock correction data for each satellite which is necessary for precise positioning. When the GPS is initially turned on, the receiver "looks" for the satellites based on where a satellite is supposed to be and as described in the Almanac and corrected for current time. The GPS determines if the Almanac data is valid; if the Almanac is not valid, possibly if the GPS receiver has been turned off for a while, the GPS searches the sky or is internally re-initialized so it can download a new Almanac from a satellite and start over. This is why a fix may take a longer time when a device is first turned on and why subsequent fixes are made more rapidly.
A GPS receiver needs a clear view of the sky to get ephemeris / almanac data from the satellites. It uses this data to determine location. It needs measurements from a minimum of three satellites to provide positional information. GPS receivers generally are capable of using from 12 up to 20 satellites to provide accurate information. Several factors determine how many satellites are used by the GPS in a fix. A limited number of satellites might be visible to the GPS, some satellites could be out of service, or objects may be blocking the satellite and reduce its broadcast signal strength. In practice, most GPSes use fewer then 9 or 10 satellites to provide a location fix.
Wireless networks use triangulation to determine the location of a device. The location information is obtained more rapidly than the data obtained from a GPS fix. A GPS fix can take from a few seconds to a minute or so with single channel GPS receivers. A network fix is very fast. However the data provided by wireless networks is less reliable than most GPS fixes. Mobile phones get the cell tower IDs from the nearest three or four cell towers in the vicinity. The technology uses the time difference of arrival to get your “exact” location within 10-50 meters of accuracy within 10-15 seconds. This method is called MS-Assist for GSM phones. A fallback method is called Cell ID. Cell ID is a passive guess of about 1.7 to 8 km (1-5 miles) in accuracy and is possibly used if MS-Assist doesn't report sufficient certainty to get a position fix less than 100 meters (the minimum performance threshold for MS-Assist).
A mobile devices uses a GPS to get an accurate snapshot of the its location while moving or stationary. Wireless networks can be used for stationary positions in the absence of a GPS receiver. The information provided in this tutorial regarding these systems may be out of date as technology improves. Check online for further improvements to GPS and wireless network positioning systems.
To make a successful geolocation based app, the developer has to know how the GPS satellite system works and consider the limitations that exist due to the quality of GPS receivers in mobile devices.
In review, the GPS receiver in most phones is not precisely accurate. The positional information it provides may be only +/- 50 meters on average, perhaps as good as +/-5 meters on occasion. The Accuracy property tells you how reliable any single satellite fix is. The GPS receivers in phones are not very sensitive. The built-in GPS loses signals in buildings and can be difficult in an urban environment. Not all tablets have a GPS receiver. Those tablets that do not have a GPS receiver have a much reduced positional accuracy compared to those that have a GPS. Devices without a GPS receiver can use triangulation between cell phone towers and/or a wifi location to approximate the phone's location.
The initial satellite fix on most devices takes about 30 to 40 seconds to achieve. Subsequent satellite fixes usually take about 20 seconds on most devices. When a developer sets the LocationSensor.TimeInterval to 1000ms (1 second), the LocationChanged event will trigger but it will not update the location once a second. A very reasonable TimeInterval might be 20000 ms.
There is an option in the location sensor to determine proximity to a destination. Experiments indicate this feature is not very accurate when set to small distance changes. About +/- 50 meters reliability might be possible on a regular basis using the LocationSensor.DistanceInterval property. Usually, the GPS 'fires' based on time but you can also allow the location sensor to 'fire' based on a distance moved. I can see an application mixing the two methods. The location will trigger a GPS fix based on time but it will also attempt to trigger based on movement since the last fix. A ten meter sampling of a location does not seem practical. because a phone GPS might only be able to resolve +/- 50 meters. See for yourself. Use the LocationSensor1.Accuracy block and sample the accuracy reading every minute and see what you get? The GPS Accuracy Logger does this so the code is available in this tutorial.
Read the MIT App Inventor's team description of the location sensor here http://appinventor.mit.edu/explore/content/sensors.html.