Using Location Information and GPS for finding your position

This tutorial introduces location services features available in App Inventor.

A future tutorial will demonstrate these features applied to specific applications.

About Android Device Location and GPS Features

Your Android device probably has built-in features to identify the device’s location. These features may include:

  • Global Positioning Satellite receiver (GPS)
  • Cellular network location information
  • Wi-Fi network location information

The GPS receiver interprets special (and very weak!) timing signals from satellites and uses those signals to compute latitude, longitude and altitude of your device. GPS has fairly good accuracy – correctly identifying the device’s position within as little as 5 meters (but which may at times be much wider such as within a 30 meter diameter circle accuracy). GPS accuracy is affected by the device’s ability to see the sky and may be blocked by buildings, trees and weather, and signals may suffer from ionospheric effects.

Cell phones can obtain information about the cellular network tower to which they are connected, including location information from the cellular tower.

Wi-Fi networks have a unique identifier associated with them (not the SSID public address). Google has built a map of wi-fi access point network locations using a combination of their Google mapping cars as well as everyone’s Android-powered cellular phone to build a huge database of wi-fi access points and their location. Your Android device can use a combination of location data and fetch a postal mailing address from Google for a given location.

Android phones have options as to which type of location information to use.

  • Cellular network information may be the least accurate, but it also uses the least amount of battery power.
  • Wi-fi network position information also uses little battery power, and can provide reasonably accurate location information when your phone is indoors and unable to hear signals from GPS satellites.
  • GPS provides the best accuracy but receiving and processing the signals uses the most battery power.

Your Android phone has options to select High accuracy, Battery saving and Device only.

  • High accuracy – Android will automatically select GPS, cellular or wi-fi to obtain a high accuracy position reading. This is likely the default setting on your phone.
  • Battery saving – Android will use cellular or Wi-fi networks to obtain position information.
  • Device only – Android will use the GPS receiver (greatest battery drain)

To select Location services, go to Settings | Location and set this feature to “On”.

Touch the item labeled “Mode” at the top of the screen to select “Location mode” options. This is where you may choose High accuracy, Battery saving or Device only.

Screenshot_20160516-175035To use any location features, you must turn Location to “On” in Android Settings!

MIT App Inventor has a component named LocationSensor providing access to latitude, longitude, altitude and even the postal mailing address associated with the location.

This tutorial introduces the LocationSensor and the basic properties and features. A future tutorial will go in to more depth.

User Interface View

This demonstration app has one button: Display Location Info.

When pressed (there may be a pause while location data is collected), all of the location data is displayed on screen. I created this app to show the location control properties and the types of data that can be obtained.

I captured the screen shot below after stepping outside and my phone was not yet locked on to GPS satellite signals, thus the data is not yet valid – you can check the HasLong-Lat property – which is false in this screen shot – to determine if position data is available.

Depending on how many satellites are immediately in view, your phone might not initially have altitude data – check the HasAltitude property to determine if the value is valid. (Read Issues With GPS Altitude and Location, below, for more information.)

Screenshot_20160516-174620Once Android’s internal location services identifies your location, it returns information to the app. As you see below, location services returns not only latitude, longitude and altitude, but if possible, even provides a postal mailing address (obscured in this screenshot since that is my house!)

Screenshot_20160516-174919In the above, note that the location Providers are “passive”. This means Android has determined location from the cellular base station or a Wi-Fi network access point, rather than a GPS signal.

In the screen below, the device has access to both GPS and “passive” network location data.

Screenshot_20160516-175111 copy

Issues With GPS Altitude and Location

The accuracy of the GPS Altitude information may be poor depending on how many satellites are in view and other factors. The same is true for the location data. In normal use, GPS position information may be off by 20 to 30 meters, but in good conditions may be accurate within meters.

Surveyors that need high accuracy GPS data use special systems to correct for position errors. One way to correct the errors is to place a 2nd GPS receiver at a precisely known location. As the GPS position information is received, it is compared to the precisely known location. If the GPS position is, say, 13 meters to the left and 20 meters too high in altitude, then the 2nd GPS receiver knows that all GPS units in the nearby vicinity are also likely to be off by 13 meters left and 20 meters too high. This “known position” GPS unit transmits these correction values (13 meters, 20 meters) to the surveyor’s field unit. The field unit GPS receiver then corrects the values it is receiving from the satellites, producing greater accuracy for the surveyor’s measurements.

Another version of this concept is known as Differential GPS or DGPS. The idea is the same but the DGPS “known position” receiver may transmit corrections over a wider area, such as a large bay or ship port. In this way, ships may obtain highly accurate GPS position information when arriving or departing the port.

In another form of DGPS, errors are measured from a wide variety of stations through the continent and these errors are relayed to a satellite ground station. These measurement errors are then transmitted to a satellite that relays the signals over much of the continent-this is known as the Wide Area Augmentation System or WAAS. GPS receivers pick up both the GPS satellites and the WAAS satellite signal and use the provided data to improve the accuracy of the raw GPS data.

DGPS and WAAS can, in theory, obtain location accuracy as low as 1 meter.

Another technique that I once used was to collect the GPS data signals over a lengthy period of time and average the measurements together. Assuming measurement errors are random – sometimes too low and sometimes too high, or sometimes shift to the left and sometimes shifted to the right – an average taken over time should lead to an improvement in the accuracy of the location.

Another method used for determining altitude is to measure barometric pressure. Inexpensive altitude measurement systems used in cars or while hiking may use this method. Barometric systems need to be periodically calibrated to a known barometric pressure or altitude (elevation).

Consequently, GPS, DGPS, and barometric altitude measurements may produce different results!

To confuse you a bit more, we tend to use elevation, altitude and height as if they mean the same thing. Surprisingly, they are not identical! To learn more, read this paper.

Designer View

To set up the demo app, drag a button on to the Designer View, followed by a textbox:

GPSDemo_DesignerDrag a LocationSensor component from the Sensors section of the palette. The LocationSensor is a non-visible component. Rename the components as shown here:

GPSDemo_ComponentsSelect the text box and in Properties, check the MultiLine property:

GPSDemo_PropMultiLineBlocks View

All our demo application does is display the LocationSensor properties. We do this by using nested join blocks to place all the labels and data values into the text box on the display:


There is not much to this app!

Future apps will demo some things we can do with the location sensor!

Source Code

Download here: GPS_Demo.aia (right-click or ctrl-click your mouse and choose save as to copy to your computer and then upload to MIT App Inventor.)

The sample app is also available in the MIT App Inventor Gallery –> here.

E-Books and Printed Books

If you find these tutorials helpful (I hope you do!) please take a look at my books on App Inventor. To learn more about the books and where to get them (they are inexpensive) please see my App Inventor Books page.

  • App Inventor 2 Introduction (Volume 1 e-book)
    Step-by-step guide to easy Android programming
  • App Inventor 2 Advanced Concepts (Volume 2 e-book)
    Step-by-step guide to Advanced features including TinyDB
  • App Inventor 2 Databases and Files (Volume 3 e-book)
    Step-by-step TinyDB, TinyWebDB, Fusion Tables and Files
  • App Inventor 2 Graphics, Animation and Charts (Volume 4 e-book)
    Step-by-step guide to graphics, animation and charts

Thank you for visiting! — Ed


3 thoughts on “Using Location Information and GPS for finding your position

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s