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"?>
<activity android:name=".GPS" android:label="@string/app_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
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:
- provider: The name of the provider with which you register
- minTime: The minimum time interval for notifications, in milliseconds.
- minDistance: The minimum distance interval for notifications, in meters.
- listener: An object whose onLocationChanged() method will be called for each location update.
The MyLocationListener class (shown shortly) implements the LocationListener abstract class. There are four methods that you need to override in this implementation:
- onLocationChanged(Location location): This method is called when the location has changed.
- onProviderDisabled(String provider): This method is called when the provider is disabled by the user.
- onProviderEnabled(String provider): This method is called when the provider is enabled by the user.
- onStatusChanged(String provider, int status, Bundle extras): This method is called when the provider status changes.
In this example, you're more interested in what happens when a location changes, so you'll write some code in the onLocationChanged()
method (Listing 2
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.