Welcome to MOTODEV  |  Join  |  Log In
 

Run WhereAreMyFriends on Android SDK 1.5

We've been learning Android, and one interesting sample we've found is WhereAreMyFriends by Reto Meier, Google Android evangelist and author of Professional Android Application Development. WhereAreMyFriends uses location, Google Maps, and data stored in Contacts, tasks that most Android developers want to learn early on. WhereAreMyFriends maps your current location by GPS, takes your friends' addresses from the Contacts application, and calculates the distance between you.

Before you look at WhereAreMyFriends, we suggest you work through the standard Android tutorials—Hello World, a few of the Hello Views tutorials, and Notepad. Once you've done those, and you're ready to see maps, contacts, and location combined in one application, come back to WhereAreMyFriends.

The sample is available at code.google.com. You can easily set it up and run it on Android SDK 1.5. Once you have the SDK installed, including Eclipse and the Android Development Tools (ADT) plugin, the first step is to download or check out the source code.

Then follow these steps, using the Android 1.5 SDK:

  1. Open a command window, navigate to your <sdk>/tools directory, and enter:
    android list targets
    You should see a target with the name Google APIs.
  2. Be sure you have an Android Virtual Device (AVD) set up for the Google APIs target. You can check by entering android list avd.
    If you don't have an AVD for Google APIs, create one according to these instructions.
  3. Open Eclipse and create an Android project. Enter a project name, click Create project from existing source, browse to the folder that contains the source files, and choose Google APIs as the build target. Change the application name, if you like.
  4. Get a Google Maps API key using the Android 1.5 SDK debug certificate. If you have an API key from a previous SDK version, you need to get a new one using the Android 1.5 SDK. (This API key works for installing the application on the emulator, but you may need a new key to install the application on a device.)
  5. Open res/layout/map-layout.xml. Locate the com.google.android.maps.MapView element and insert your API key as the value of the android:apiKey attribute:
    android:apiKey="yourAPIKeyHere"
  6. In the command window, start the emulator using the AVD for the Google APIs target:
    emulator -avd <avdName>
  7. Open the Contacts application. Create a few contacts, giving each a home address using Edit contact  >  Postal addresses  >  Home.
  8. Back in Eclipse, go to the DDMS perspective. In the Devices view, click the emulator name, for example, emulator-5554.
  9. In the Emulator Control view, enter the longitude and latitude of your current location—or just use the default values, which are for Google headquarters in Mountain View, California. Click Send to send your location to the emulator.
  10. Return to the Java perspective.
  11. Create a run configuration for your project using Run  > Run Configurations. In the Target tab, select the AVD for the Google APIs target.
  12. Click Apply, then Run. Wait for the application to be deployed to the emulator. You should see a map with your current location, with lines drawn to your friends' locations.
  13. Click Menu, then Show Distances. You should now see a screen listing distances between you and your friends.

You can now add more contacts, pan around the map, and show distances. WhereAreMyFriends uses a LocationManager to get your current position and a ContentResolver to interact with the Contacts content provider. To learn more about the code in WhereAreMyFriends, see this blog post by Reto Meier.

Have fun with it!

– Code sample by Reto Meier.


Was This Document Helpful?
Yes  No 

Additional Comments (Optional)



Submit

Share This Page

Bookmark and Share