Location-based services have changed the way we navigate, share, and shop.
Since the FCC ruling in 1996 requiring all US mobile operators to locate emergency callers, location has become embedded in the images we take, the articles we blog, the commercials we watch, and places we check-in to. These services rely on location information, using latitude and longitude—and sometimes altitude—to describe our north-south and east-west position on the Earth’s surface.
When we search for local information, get directions to public transportation, find the nearest bar or bargain, the Android enables us to zero in on informa-tion that is relevant to us at a particular geographic locainforma-tion. Because the device is aware of its own geolocation, we can navigate, detect where we are heading, and know how we are holding the device in relation to magnetic North. A built-in GPS receiver, accelerometer, and digital compass allow the Android to have a full picture about its location and orientation, which plays an important role for navigation apps and location-based services.1
Android apps make use of the Android’s Location Manager2 to calculate a location estimate for the device. Its purpose is to negotiate the best location source for us, and keep the location up-to-date while we are on the move.
Updates typically occur when the device detects that we’ve changed location or a more accurate location becomes available. An Android device uses two different location providers3 to estimate its current geographic coordinates:
GPS on the one hand, and network on the other, the latter based either on
1. http://en.wikipedia.org/wiki/Location-based_service
2. http://developer.android.com/reference/android/location/LocationManager.html 3. http://en.wikipedia.org/wiki/GSM_localization
the calculated distance to multiple cell towers, or the known location of the WiFi network provider to which we are connected.4
Compared with GSM and Wi-Fi network localization, GPS is the most well-known and accurate method for determining the location of a device. With 31 GPS satellites orbiting about 20,000 kilometers above the Earth’s surface, twice a day (every 11 hours 58 minutes), it’s just fantastic how the fingertip-sized GPS receivers built into our smart phones are able to determine the device latitude, longitude, and altitude at a theoretical accuracy of about three meters.
In this chapter, we’ll build a series of navigation apps. We’ll start by working with the Android’s current geolocation. We’ll continue by measuring how far we are located from a pre-defined destination. Then we’ll build an app that helps us navigate towards another mobile device. To make this possible we need to post our location and that of the other device at a place to which both devices have access. We’ll use a web server for that purpose.
4.1 Introducing the Location Manager
Given its ubiquitous use, working with geolocation data should be simple. In the end, it’s just the latitude, longitude, and maybe altitude we are looking to incorporate into our apps. Because there are various location techniques however, it’s a fairly complicated system we are interacting with, continuously negotiating the best and most accurate method to localize the device. The Location Manager, which does that work for us, is a software class that obtains periodic updates of the device’s geographical location from three sensors available on an Android phone or tablet, including a built-in GPS receiver, and cellular and WiFi radios. Both the KetaiLocation and Android Location class draw their data from the Location Manager which gets it information, in turn, from the on-board devices.
The Global Positioning System or GPS, which you’ll learn about in detail on page 81, can provide a fairly precise location estimate. It is however not available indoors, or if a building obstructs a direct "view" of GPS satellites overhead. In addition, the GPS receiver also uses a significant amount of battery power.
Another localization method uses cellular tower signals to determine the location of a device by measuring the distances to multiple towers within
4. http://developer.android.com/guide/topics/location/obtaining-user-location.html
reach. This triangulation5 method is a less precise, because it depends on weather conditions and relies on a fairly high cell tower density.
The third method doesn’t require GPS or cell towers at all, but the presence of a Wi-Fi network. This technique uses the known locations of nearby Wi-Fi access points to figure out the approximate location of the mobile device. Wi-Fi access points themselves lack GPS receivers and therefore lack knowledge of their geographic location, but such information can be associated with their physical MAC (Media Access Control) addresses by third parties. The most notorious case was Google’s now abandoned effort to associate GPS coordinates with the MAC address of every wireless access point it encountered as it photographed the streets of U.S. cities and towns with GPS-enabled vehicles for its Google Maps Street View project. 6 of WiFi routers, amongst other things.7
Nowadays, we’re the ones who do this work for Google whenever we take an Android device for a stroll. If we have activated Google's location service by selecting Settings→Location services on the main menu of our device, then by default we have agreed "to collect anonymous location data" and that "collection may occur even when no apps are running". The MAC addresses of available Wi-Fi networks are sent to Google during this collection process, along with their geographic coordinates. The next user who walks through the same geographic area can then geolocate solely via the Wi-Fi network information, even if GPS is turned off.
It takes a few seconds for the Android to narrow the location estimate and improve its accuracy, so we typically need to start KetaiLocation as soon as the app launches. With fewer than ten lines of code, KetaiLocation can provide us with our geographic coordinates, and notify us of changes in our location via the onLocationEvent() callback method.
For the location-based apps we’ll develop in this chapter, we’ll use the following Ketai library and Android Classes:
KetaiLocation8 A class that simplifies working with Android’s Location Manager.
It instantiates the Location Manager, registers for location updates, and returns geolocation data.
5. http://en.wikipedia.org/wiki/Triangulation 6. http://en.wikipedia.org/wiki/MAC_address
7. http://www.nytimes.com/2012/05/23/technology/google-privacy-inquiries-get-little-cooperation.html, http://googleblog.blogspot.com/2010/05/wifi-data-collection-update.html, http://consumercal.blogspot.com/
2010/06/google-wi-spy-scandal-update-company.html
8. http://ketai.googlecode.com/svn/trunk/ketai/src/ketai/sensors/KetaiLocation.java
Introducing the Location Manager
•
79Location9 A wrapper for Android Location Manager that provides us with many useful methods for determining our position, bearing and speed. If we’re only interested in our location, we won’t need this class, but we will use some of its features for later projects in this chapter.
Now let’s take a look at the KetaiLocation methods we’ll be using in this chapter.
4.2 Working With the
KetaiLocationClass
The KetaiLocation class is designed to provide us with the longitude, latitude, and altitude of the device, as well as the accuracy of that estimate. Besides the typical start() and stop() methods, KetaiLocation also provides a method to identify the location provider that has been used to calculate the estimate.
Let’s take a look:
onLocationEvent() Returns the device location, including latitude, longitude, altitude, and location accuracy.
latitudeDescribes the angular distance of a place north or south of the earth’s equator in decimal degrees. Positive lat values describe points north of the equator; negative values describe points south of the equator (i.e.
Chicago is located at the latitude 41.87338 on the northern hemisphere;
Wellington, New Zealand is located at the latitude -41.29019 on the southern hemisphere).
longitude Describes the angular distance of a place east or west of the meridian at Greenwich, England, in decimal degrees (i.e. Chicago, which is west of the Greenwich meridian, is located at the longitude -87.648798; Xinjiang Province, China is located at the longitude 87.648798.
altitude Returns the height of the device in relation to sea level measured in meters.
accuracy Returns the accuracy of the location estimate in meters.
getProvider() Returns the identity of the location provider being used to estimate the location: gps or network. It does not distinguish between cellular or WiFi networks.
Let’s go ahead and write our first location-based app.