CPSC 110-08: Computing on Mobile Phones
Spring 2011

The Location Sensor

This tutorial will show how to use the Android's location sensor.


There is a nice concise discussion of how to use App Inventor's sensor's in Chapter 23 of Prof. Wolber's textbook.

Prof. Wolber has also written a nice tutorial on this subject. We will be customizing that tutorial.

Global Positioning System (GPS)

GPS data comes from the Global Positioning System, a satellite system maintained by the US government. To get a GPS reading your phone must have an unobstructed line-of-sight to three satellits.

A GPS reading consists of three data: latitude, longitude, and altitude. Your latitude is your distance in degrees north or south of the equator, with north positive and south negative. The range is -90 to +90.

Your longitude is your distance east or west of the Prime Meridian, which goes through Greenwich England. East coordinates have positive values and west coordinates are negative. The values range from -180 to +180. Values on the Meridian are 0.

Android's Location Sensor

There are three ways that your Android phone can determine its location:
  1. GPS Satellites are accurate to within a few meters, but they only work out doors.

  2. WiFi: If you are on a WiFi network, the phone can use the location of the WiFi hot spot as its location.

  3. Cell Towers can also be used to locate the phone based on a calculation of the phone's proximity to known cell phone towers.

Where Are We?

We can use Google Maps to find our current GPS coordinates. Navigate and zoom in to your current location on the map. Using satellite view makes it easy to find your exact location. Click on the Link tab in the top right. This will display something that looks like this:
Those coordates, (41.744416,-72.691043), are on the Trinity College campus.

Using the Location Sensor in App Inventor

App Inventor provides a LocationSensor component. It is an invisible component in the Sensors drawer.

Let's set up the following interface in the App Inventor Designer that includes a LocationSensor. It contains several labels, a button, and LocationSensor:

Using the LocationSensor in your app is primarily a matter of responding to location change events using the LocationChanged event handler. In this first example, whenever the sensor detects a change in location, the event handler simply display's the phone's latitude and longitude and speaks its current address.

Of course, in order to test this app, you will have to download it to your phone and then walk around outdoors.

Enabling and Disabling the LocationSensor

Of course we can also control when location readings are taken, as the following example shows. Here, the LocationSensor is enabled whenever the user clicks the button. Once a reading is received, it is disabled.

Homework Assignment

For this homework assignment you will download and play with a version of the location demo. Use the following QR code to download the app to your phone.

Use Google maps to choose a rectangular section of the Trinity campus and then use the app to find the GPS coordinates of the four corners of the rectangular region -- i.e., top-left, top-right, bottom-left, and bottom-right. Next class we'll use the coordinates to improve the app to show our location on the Trinity map.

Here's the app's sourcecode. Download it and play with it before the next class.

  • That's It!