he previous DevX article “Augmented Reality on Android: Prepping the Camera and Compass” explored how to use the Android SDK’s camera and compass as the first two building blocks of an Augmented Reality (AR) engine. This follow-up article details the last two pieces of the puzzle: location and the accelerometer. With the necessary tools in hand, you will learn how to request location updates and find out how the accelerometer works.
|What You Need|
|Android SDK 1.5|
|T-Mobile G1 phone or equivalent emulator|
|Eclipse with Android Development Tools (ADT) Plugin, NetBeans, or the IDE of your choice|
Talking to the Satellites
The third step along the way to your Augmented Reality Engine on Android?after implementing the first two elements: the camera and the compass?is determining your location. You do this primarily through Android’s LocationManager object. Before you can get one, however, you’ll need to jump through a few hoops. Permissions, while annoying, are an important hurdle you’ll need to clear.
Android’s LocationManager carries two permission requirements:
- You need to tell the system you’d like to fetch the user’s location.
- You need to tell it that you want very detailed geographic information.
You request both permissions in the AndroidManifest.xml file with the
For fine-grain location updates, which you’ll need if the objects you want to display are nearby, you’ll also have to add the following:
Without these two lines in the manifest, when you attempt to register for location updates, Android will return a security exception that will stop your app dead. I don’t believe I’ve written an Android application yet in which I haven’t forgotten at least one permission request.