n the previous article in the Android series, you learned how to integrate the Google Maps into your Android application. One of the really neat ways you can use Google Maps is to send GPS data directly into it so that you can view your current location real-time. This article will show you how to programmatically access the data returned by your built-in GPS receiver and then send the data to Google Maps.
Creating the Project
Using Eclipse, create a new Android project and name it GPS.java.
To use GPS functionality in your Android application, you'll need to add the ACCESS_FINE_LOCATION permission to the AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.learn2develop.GPS">
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".GPS" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
In Android, location-based services are provided by the LocationManager class located in the android.location package. Using the LocationManager class, your application can obtain periodic updates of the device's geographical locations as well as fire an intent when it enters the proximity of a certain location.
In the GPS.java file, first obtain a reference to the LocationManager class using the getSystemService() method. To be notified whenever there is a change in location, you need to register for a request for changes in locations so that your program can be notified periodically. This is done via the requestLocationUpdates() method (see Listing 1). This method takes in four parameters:
Specifically, when a location changes you will display a small dialog on the screen showing the new location information: latitude and longitude. You show this dialog using the Toast class. The complete GPS.java now looks like Listing 3.
To test the application, press F11 in Eclipse to debug the application on the Android emulator. While at the time of writing this article, you may not have a real Android device to test, there are a number of ways to test GPS functionality on your Android application.
Testing GPS Functionalities
The DDMS tool in the Android plug-in for Eclipse allows you to test GPS functionality very easily. In Eclipse, switch to the DDMS view and locate the Location Controls section in the Emulator Control tab (see Figure 1).
![]() Figure 1. The Location Controls Section: This section allows you to test GPS functionality on the Android emulator. | ![]() Figure 2. The New Location Information: When the GPS data is received, the application displays the latitude and longitude obtained. |
There are three separate tabs in the Location Controls section. First, you can manually send in the coordinates by specifying the latitude and longitude. When the GPS data is received on the Android emulator, the application will display the latitude and longitude obtained (see Figure 2).
Another way to send in geographical locations is to use a .GPX file. GPX (GPS Exchange Format) is a light-weight XML data format for interchange of GPS data. You can download GPS samples here. Once a .GPX file is downloaded, click the Load GPX… button to load the .GPX file (see Figure 3).
You can click the Play button to send a series of coordinates to the Android emulator at regular time intervals. The Android Eclipse plug-in also supports KML (Keyhole Markup Language) files. You can download a sample .KML file here. Like the .GPX file, you can also send a series of coordinates to the Android emulator by clicking on the Play button (see Figure 4).
![]() Figure 3. Loading a .GPX File: Click the Load GPX button to load the .GPX file. | ![]() Figure 4. Loading a .KML File: Sending coordinates to the .KML file. |
If you don't use Eclipse, you can also telnet to the Android emulator and use the geo command to send GPS coordinates to the emulator:
C:\>telnet localhost 5554
Android Console: type 'help' for a list of commands
OK
geo fix -82.411629 28.054553
OK
The geo command also allows you to send NMEA data sentences, like this:
geo nmea $GPGGA,001431.092,0118.2653,N,10351.1359,E,0,00,,-19.6,M,4.1,M,,0000*5B
Using GPS with Google Maps
Simply displaying the latitude and longitude when a location has changed is not very interesting. A much more interesting thing to do would be to couple the data together with the Google Maps application.
As you learnt in the previous article, for Google Maps to work, you need to add the ACCESS_FINE_LOCATION permission and then use the Google Maps library (see Listing 4).
In main.xml, replace the <TextView> element with the <MapView> element:
![]() | |
| Figure 5. You Are Here: Displaying the current location using Google Maps. |
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.google.android.maps.MapView
android:id="@+id/mapview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="apisamples" />
</LinearLayout>
Finally, modify the GPS.java file to incorporate Google Maps (see Listing 5).
In the above, when a location changes, the latitude and longitude is sent to the Google Maps application, which then displays the map of the current location (see Figure 5).
Summary
You've seen how to make use of the built-in GPS receivers in Android devices to do some very interesting things. Besides displaying maps, you can also log your positions by saving the GPS coordinates into a text file. This will enable you to retrieve them at a later date.
| DevX is a division of Jupitermedia Corporation © Copyright 2007 Jupitermedia Corporation. All Rights Reserved. Legal Notices |